function generateTestArray() {
const result = [];
for (let i = 0; i < 1000000; ++i) {
result.push({
a: i,
b: i / 2,
r: 0,
});
}
return result;
}
const array = generateTestArray();
array.forEach((x) => {
x.r = x.a + x.b;
});
const array = generateTestArray();
for(let index = 0; index < array.length; index++) {
array[index].r = array[index].a + array[index].b;
}
const array = generateTestArray();
for(let index = 0; index < array.length; index++) {
const value = array[index]
value.r = value.a + value.b;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.forEach | |
for | |
for name first |
Test name | Executions per second |
---|---|
.forEach | 14.3 Ops/sec |
for | 21.2 Ops/sec |
for name first | 21.0 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Definition Json:
The provided JSON defines a JavaScript microbenchmark that measures the performance of two different approaches to iterate over an array:
for
loop.forEach()
methodIn the Script Preparation Code
, we see a function generateTestArray()
that generates a large array with 1 million elements, each containing three properties: a
, b
, and r
. The generateTestArray()
function is used to create an array for testing.
Individual Test Cases:
There are three test cases:
.forEach()
: This test case uses the .forEach()
method to iterate over the generated array, updating each element's property r
with the sum of its properties a
and b
.for
: This test case uses a traditional for
loop to iterate over the generated array, also updating each element's property r
with the sum of its properties a
and b
. However, this test case has an additional difference: it uses variable interpolation (const value = array[index]
) instead of directly accessing the array elements.for name first
: This test case is similar to the for
loop test case, but with another minor difference: it also uses variable interpolation (const value = array[index]
) instead of direct array access.Options Comparison:
The two main options being compared are:
.forEach()
methodfor
loopPros and Cons:
.forEach()
method:for
loop:Library:
There is no explicit library mentioned in the provided benchmark definition or test cases. However, some browsers might have internal optimizations or features that could influence the performance results (e.g., V8 engine's optimization for forEach()
).
Special JS Feature/Syntax:
Variable interpolation (const value = array[index]
) is used in two of the test cases (for
and for name first
). This feature allows dynamic referencing to array elements, but its impact on performance may vary depending on the specific JavaScript engine and browser implementation.
Other Alternatives:
Other iteration methods that could be considered for benchmarking include:
for...of
: A more modern and concise iteration method introduced in ECMAScript 2017.forEach()
pattern.Keep in mind that the choice of iteration method often depends on the specific problem requirements and personal preference.