Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Methods to remove duplicates object with a key from array
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser:
Chrome 122
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Using a Set
801.4 Ops/sec
Using filter and findIndex
428.6 Ops/sec
Using reduce
450.6 Ops/sec
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push({ id: Math.floor((Math.random() * 10) + 1), someOtherKey: "someOtherValue" }); }
Tests:
Using a Set
function removeDuplicates(items) { const seenIds = new Set(); const result = []; for (const item of items) { if (!seenIds.has(item.id)) { seenIds.add(item.id); result.push(item); } } return result; } removeDuplicates(array)
Using filter and findIndex
array.filter((item, index, array) => array.findIndex(i => i.id === item.id) === index );
Using reduce
array.reduce((acc, currentItem) => { const isDuplicate = acc.some(item => item.id === currentItem.id); if (!isDuplicate) { acc.push(currentItem); } return acc; }, []);