window.objects = []
window.arrays = []
for(let i = 0; i < 1000000; i++){
objects.push({a: true, b: true, c: true});
arrays.push([true, true, true]);
}
objects.forEach(obj => Object.entries(obj).forEach(() => {}));
arrays.forEach(array => array.forEach(() => {}))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Objects | |
Arrays |
Test name | Executions per second |
---|---|
Objects | 2.3 Ops/sec |
Arrays | 83.2 Ops/sec |
Let's break down the provided benchmark json and explain what's being tested.
Benchmark Definition
The benchmark is testing two approaches: iterating over object entries using Object.entries()
or iterating over array elements using the built-in forEach()
method.
Options Compared
Two options are compared:
Object.entries()
and then calling a callback function on each entry.forEach()
method and then calling a callback function on each element.Pros and Cons
Both approaches have their trade-offs:
forEach()
method.Other Considerations
The benchmark prepares two arrays (window.objects
and window.arrays
) with 1 million elements each. The scripts use a simple loop to push objects or array elements into these arrays. The HTML preparation code is empty, which means that the only resource used by the benchmark is the JavaScript engine.
Library and Special JS Feature
There are no libraries mentioned in this benchmark. However, it's worth noting that Object.entries()
was introduced in ECMAScript 2015 (ES6) as part of the standard, so this benchmark may be testing the performance of a newer JavaScript engine like V8.
Test Case Explanation
The two test cases iterate over an array of objects or arrays using the respective methods. The callback function passed to forEach()
is not executed for any reason in this case; it's likely that the focus is on measuring the iteration overhead rather than processing the elements.
Alternatives
Other alternatives to measure iteration performance could include:
for
loops instead of forEach()
.Keep in mind that this is just one possible way to design a benchmark for iteration performance, and there are many other approaches you could take depending on your specific use case and requirements.