Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dedup Array of Objects (4 strats)
(version: 1)
Comparing performance of:
Filter + Set vs Array from Map vs Naive Filter vs Object lookup
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const ids = Array.from({ length: 1000 }, () => Math.random().toString(16).slice(2)); const array = [...ids.map((id) => ([{ id: id, content: "foo" }, { id: id, content: "bar" }, { id: id, content: "baz" }]))].flat();
Tests:
Filter + Set
const seen = new Set(); const deduplicatedArray1 = array.filter(item => { if (seen.has(item.id)) { return false; } seen.add(item.id); return true; });
Array from Map
const deduplicatedArray2 = [ ...new Map(array.map(item => [item.id, item])).values() ];
Naive Filter
const deduplicatedArray3 = array.filter((item, index, self) => index === self.findIndex(obj => obj.id === item.id) );
Object lookup
const deduplicatedArray4 = []; const seen = {}; for (const item of array) { if (!seen[item.id]) { deduplicatedArray4.push(item); seen[item.id] = true; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Filter + Set
Array from Map
Naive Filter
Object lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter + Set
15587.9 Ops/sec
Array from Map
11636.0 Ops/sec
Naive Filter
27.3 Ops/sec
Object lookup
16008.3 Ops/sec
Related benchmarks:
Unique values (large list)
WHateverfdsgffgdfg
Deduplicate array test
Deduplicate array test v2
Test spreads1
flatmap vs concat and spread operator
Trey - unique array
Filtering duplicates
Set vs Filter for unique (Array of Objects)
Comments
Confirm delete:
Do you really want to delete benchmark?