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(const x of array) {
x.r = x.a + x.b;
}
const array = generateTestArray();
array.map(x => x.a + x.b)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.forEach | |
for..of | |
.map |
Test name | Executions per second |
---|---|
.forEach | 11.9 Ops/sec |
for..of | 15.1 Ops/sec |
.map | 9.0 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations.
Benchmark Definition The benchmark is designed to compare the performance of three JavaScript iteration methods:
.forEach
for..of
.map
These methods are used to iterate over an array and perform some operation on each element.
Script Preparation Code
The script preparation code generates a large test array with 1,000,000 elements, where each element is an object with three properties: a
, b
, and r
. The purpose of this array is to serve as the input for the iteration methods being tested.
Html Preparation Code There is no HTML preparation code provided, which means that only JavaScript execution time is measured.
Individual Test Cases Each test case represents a single iteration method:
.forEach
: Uses the .forEach
method to iterate over the array and update each element's r
property by adding its a
and b
properties.for..of
: Uses the for..of
loop syntax to iterate over the array and update each element's r
property by adding its a
and b
properties..map
: Uses the .map
method to create a new array with updated elements, where each element is an object with three properties: a
, b
, and r
.Library Used None of the test cases use any external libraries.
Special JS Feature/Syntax The benchmark uses modern JavaScript features:
=>
) are used in .forEach
and .map
.for..of
loop syntax is used.{ ... }
) are used to create objects with multiple properties.Comparison Options
Method | Description |
---|---|
.forEach |
Iterates over the array using a callback function. Each element's r property is updated individually. |
for..of |
Iterates over the array using a loop that yields each element. Each element's r property is updated individually. |
.map |
Creates a new array with updated elements, where each element has three properties: a , b , and r . |
Pros and Cons
.forEach
: Easy to read and maintain, but can be slower than other methods due to the callback function overhead.for..of
: More efficient than .forEach
and .map
, but its syntax is less readable. Can be a good choice for performance-critical code..map
: Creates a new array with updated elements, which can lead to more memory usage compared to other methods.Other Considerations
Alternatives If you want to explore alternative iteration methods or compare their performance, you can consider:
Array.prototype.forEach.call()
instead of .forEach
for
loops with traditional indexing (i
) instead of for..of
reduce()
or other array methods like filter()
and every()
Note that these alternatives may not be as efficient as the tested methods, but can provide alternative perspectives on iteration in JavaScript.