function generateId(len) {
return [Array(len)]
.map(i => (~~(Math.random() * 36)).toString(36))
.join("");
}
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;
}, {}));
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)));
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);
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);
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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Reduce | |
Set Map | |
Set Simple For | |
Set For Of | |
Set For Each |
Test name | Executions per second |
---|---|
Reduce | 26.6 Ops/sec |
Set Map | 29.8 Ops/sec |
Set Simple For | 30.8 Ops/sec |
Set For Of | 31.0 Ops/sec |
Set For Each | 31.3 Ops/sec |
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:
Object.keys()
to find unique IDs, then uses reduce()
to create a new array with those IDs.Array.from()
and map()
to create an array of unique IDs, leveraging the built-in uniqueness of Sets in JavaScript.for
loop and has()
method on a Set object to find unique IDs.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:
Pros and cons of each approach
Here's a brief summary of the pros and cons for each approach:
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:
Map
and has()
methodsHowever, these alternatives are not represented in the provided benchmark.