<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {}
const exampleObjects = Array.from(Array(1000)).map((_, i) => {
const randomId = Math.round(Math.random() * 100);
const otherRandomId = Math.round(Math.random() * 100);
return {
objectId: i % 3 === 0 ? undefined : randomId.toString(),
clientObjectId: i % 4 === 0 ? undefined : otherRandomId.toString(),
}
});
const objectIds = new Set(exampleObjects.map(i => i.objectId).filter(i => i !== undefined));
const clientObjectIds = new Set(exampleObjects.map(i => i.clientObjectId).filter(i => i !== undefined));
const exampleObjects = Array.from(Array(1000)).map((_, i) => {
const randomId = Math.round(Math.random() * 100);
const otherRandomId = Math.round(Math.random() * 100);
return {
objectId: i % 3 === 0 ? undefined : randomId.toString(),
clientObjectId: i % 4 === 0 ? undefined : otherRandomId.toString(),
}
});
const objectIds = new Set();
const clientObjectIds = new Set();
for (const i of exampleObjects) {
if (i.objectId) objectIds.add(i.objectId);
if (i.clientObjectId) clientObjectIds.add(i.clientObjectId);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For-Each | |
map + filter twice |
Test name | Executions per second |
---|---|
For-Each | 16738.5 Ops/sec |
map + filter twice | 20723.4 Ops/sec |
The benchmark provided in the JSON consists of two different approaches to iterating over and processing an array of objects in JavaScript. The focus is on comparing two methodologies for retrieving unique identifiers from a list: using Array.prototype.map
and Array.prototype.filter
methods versus using a traditional for...of
loop.
For-Each
map
function two times, where the first maps to create an array of object identifiers (objectId
), and the second filters out undefined values. The process results in the creation of a Set
, which inherently provides unique values.const exampleObjects = Array.from(Array(1000)).map((_, i) => {
const randomId = Math.round(Math.random() * 100);
const otherRandomId = Math.round(Math.random() * 100);
return {
objectId: i % 3 === 0 ? undefined : randomId.toString(),
clientObjectId: i % 4 === 0 ? undefined : otherRandomId.toString(),
};
});
const objectIds = new Set(exampleObjects.map(i => i.objectId).filter(i => i !== undefined));
const clientObjectIds = new Set(exampleObjects.map(i => i.clientObjectId).filter(i => i !== undefined));
map + filter twice (traditional for loop)
for...of
loop. It checks each object's properties and adds valid identifiers to the respective Set
.const exampleObjects = Array.from(Array(1000)).map((_, i) => {
const randomId = Math.round(Math.random() * 100);
const otherRandomId = Math.round(Math.random() * 100);
return {
objectId: i % 3 === 0 ? undefined : randomId.toString(),
clientObjectId: i % 4 === 0 ? undefined : otherRandomId.toString(),
};
});
const objectIds = new Set();
const clientObjectIds = new Set();
for (const i of exampleObjects) {
if (i.objectId) objectIds.add(i.objectId);
if (i.clientObjectId) clientObjectIds.add(i.clientObjectId);
}
The benchmark results indicate the number of executions per second for each method:
For-Each (map + filter)
map
and filter
twice.map + filter (traditional for-loop)
Set
.for...of
) become more advantageous.Array.prototype.reduce
method to achieve a similar result in a single traversal, accumulating results directly into the Set
.const objectIds = exampleObjects.reduce((set, obj) => {
if (obj.objectId) set.add(obj.objectId);
return set;
}, new Set());
This benchmark illustrates key differences in approaches to processing data in JavaScript, emphasizing performance while highlighting readability and maintainability.