CodexBloom - Programming Q&A Platform

JavaScript - How to Efficiently Filter Unique Objects from an Array Based on a Property?

👀 Views: 92 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-22
javascript arrays performance filter data-structures JavaScript

I'm trying to figure out I'm currently working with an array of objects, and I need to filter out duplicate entries based on a specific property, `id`, while maintaining the original order... I've tried using a combination of `filter` and `map`, but I'm concerned about performance when dealing with large arrays (thousands of entries). My current approach is as follows: ```javascript const items = [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 1, name: 'Item 1 Duplicate' }, { id: 3, name: 'Item 3' }, { id: 2, name: 'Item 2 Duplicate' } ]; const uniqueItems = items.filter((item, index, self) => index === self.findIndex((t) => (t.id === item.id)) ); ``` While this works, I noticed that the performance drops significantly with larger datasets. I also tried using a `Set`, but it doesn't retain the object structure, which is vital for my use case. Here's the code I attempted: ```javascript const uniqueIds = new Set(); const filteredItems = items.filter(item => { if (uniqueIds.has(item.id)) { return false; } else { uniqueIds.add(item.id); return true; } }); ``` This method did improve performance a bit, but I'm still worried about the overall complexity. Can anyone suggest a best practice or an efficient way to achieve this filtering while keeping the original order and ensuring good performance? Any insights on the time complexity or alternative methods would be very appreciated. I'm using Node.js v16.x for reference. This issue appeared after updating to Javascript stable. How would you solve this? What would be the recommended way to handle this?