function setup() {
const randInt = (min, max) =>
Math.floor(Math.random() * (max - min + 1) + min);
const itemMap = {};
const itemArr = [];
for (let i = 0; i < 10000; i++) {
const item = {
id: randInt(0, 1000000),
value: `value-${randInt(0, 1000000)}`
};
itemArr.push(item);
itemMap[item.id] = item;
}
const itemIdsToGet = [itemArr[8002].id, itemArr[2423].id, itemArr[5322].id, itemArr[3].id, itemArr[7242].id]
return {
itemIdsToGet,
itemMap,
itemArr
}
}
const values = setup();
for (const itemIdToUpdate of values.itemIdsToGet) {
values.itemMap[itemIdToUpdate].value = 'updated'
console.log(Object.values(values.itemMap));
}
const values = setup();
for (const itemIdToUpdate of values.itemIdsToGet) {
const index = values.itemArr.findIndex(x => x.id === itemIdToUpdate);
values.itemArr[index].value = 'updated';
console.log(values.itemArr);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Item map and enumerating values | |
item array |
Test name | Executions per second |
---|---|
Item map and enumerating values | 16.3 Ops/sec |
item array | 21.6 Ops/sec |
Let's dive into the benchmark.
What is being tested?
The benchmark compares two approaches to update and log the values of objects in an array versus using an object literal.
In the "Array" approach, the code iterates through the itemIdsToGet
array and finds the corresponding index in the itemArr
array. It then updates the value at that index in the itemArr
array.
In the "Object Literal" approach, the code directly accesses the object literal using the itemIdToUpdate
key.
Options being compared
The two options being compared are:
itemIdToUpdate
).Pros and Cons of each approach:
Library and purpose
There is no explicit library mentioned in this benchmark. However, the findIndex
method used in the "Array" approach is a native JavaScript method that belongs to the Array prototype.
Special JS feature or syntax
None are explicitly mentioned.
Benchmark preparation code explanation
The setup()
function creates two data structures:
itemMap
that maps item IDs to their corresponding objects.itemArr
that contains objects with id
and value
properties.The for
loop generates 10,000 random items and populates the itemArr
array. The itemIdsToGet
array is then generated by selecting a subset of item IDs from the first 8,022 items in the array (randomly chosen).
Individual test cases
There are two test cases:
itemIdsToGet
array and updates the corresponding object's value using direct object access.itemIdsToGet
array and finds the corresponding index in the itemArr
array, then updates the value at that index.Alternatives
Some alternatives to benchmarking JavaScript performance include:
Date
objects or performance.now()
to manually measure execution times.In summary, this benchmark compares two approaches to update and log the values of objects in an array versus using an object literal. The "Array" approach involves iteration through an array, while the "Object Literal" approach uses direct access to the object.