Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Object Array v2
(version: 0)
Make unique an array of objects based on a key
Comparing performance of:
Reduce vs Set Map vs Set Simple For vs Set For Of vs Set For Each
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateId(len) { return [...Array(len)] .map(i => (~~(Math.random() * 36)).toString(36)) .join(""); }
Tests:
Reduce
let data = []; for (let i = 0; i < 10000; i++) { data.push({ id: generateId(4) }); } const newDataReduce = Object.keys(data.reduce((acc, cur) => { if(acc[cur.id]) return acc; acc[cur.id] = true; return acc; }, {}));
Set Map
let data = []; for (let i = 0; i < 10000; i++) { data.push({ id: generateId(4) }); } const newDataSet = Array.from(new Set(data.map(f => f.id)));
Set Simple For
let data = []; for (let i = 0; i < 10000; i++) { data.push({ id: generateId(4) }); } const newDataSimpleFor = new Set(); for(let i = 0; i < data.length; i++){ if(newDataSimpleFor.has(data[i].id)) continue; newDataSimpleFor.add(data[i].id); } const res = Array.from(newDataSimpleFor);
Set For Of
let data = []; for (let i = 0; i < 10000; i++) { data.push({ id: generateId(4) }); } const newDataForOf = new Set(); for(let dp of data){ if(newDataForOf.has(dp.id)) continue; newDataForOf.add(dp.id); } const res2 = Array.from(newDataForOf);
Set For Each
let data = []; for (let i = 0; i < 10000; i++) { data.push({ id: generateId(4) }); } const newDataForEach = new Set(); data.forEach(dp => { if(newDataForEach.has(dp.id)) return; newDataForEach.add(dp.id); }); const res3 = Array.from(newDataForEach);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Reduce
Set Map
Set Simple For
Set For Of
Set For Each
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **What is tested?** MeasureThat.net tests four different approaches for creating an array of unique identifiers from a list of objects with identical IDs: 1. **Reduce**: Uses `Object.keys()` to find unique IDs, then uses `reduce()` to create a new array with those IDs. 2. **Set Map**: Uses `Array.from()` and `map()` to create an array of unique IDs, leveraging the built-in uniqueness of Sets in JavaScript. 3. **Set Simple For**: Uses a traditional `for` loop and `has()` method on a Set object to find unique IDs. 4. **Set For Of**: Uses a `for...of` loop and `has()` method on a Set object to find unique IDs. **Options compared** Each test case compares the performance of one approach against another, allowing users to compare the execution times of different methods. The options being compared are: * Reduce vs. Set Map * Reduce vs. Set Simple For * Reduce vs. Set For Of **Pros and cons of each approach** Here's a brief summary of the pros and cons for each approach: 1. **Reduce**: Pros - concise code, easy to understand. Cons - may be slower due to unnecessary function calls. 2. **Set Map**: Pros - efficient, built-in data structure in JavaScript. Cons - requires understanding of how Sets work, can lead to unexpected behavior if not used correctly. 3. **Set Simple For**: Pros - straightforward, easy to implement. Cons - potentially slower than Set Map, may require more code. 4. **Set For Of**: Pros - similar to Set Simple For, but with a more modern syntax. Cons - same limitations as Set Simple For. **Library and purpose** None of the test cases use any external libraries besides JavaScript's built-in functionality (e.g., Sets). **Special JS feature or syntax** The `for...of` loop is a relatively new feature in JavaScript (introduced in ES6) that allows iterating over iterable objects, such as Sets. While not specific to this benchmark, it's an interesting example of modern JavaScript's capabilities. **Other alternatives** In theory, other approaches could be used to create unique identifiers from an array of objects with identical IDs, such as: * Using `Map` and `has()` methods * Utilizing a library like Lodash or Ramda for utility functions * Implementing a custom solution using bitwise operations or other low-level techniques However, these alternatives are not represented in the provided benchmark.
Related benchmarks:
Map vs Array vs Object has uint32 key speed
Object vs Map
Large Map vs Object 2
comparing Map and object
Map vs object copying
Comments
Confirm delete:
Do you really want to delete benchmark?