var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
function someFn(i) {
return i * 3 * 8;
}
arr.forEach(function (item){
console.log(someFn(item));
})
for (var i = 0, len = arr.length; i < len; i++) {
console.log(someFn(arr[i]));
}
arr.map(item => console.log(someFn(item)))
for (var i = 0, len = arr.length; i < len; i++) {
console.log(i * 3 * 8);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for | |
map | |
for no fn call |
Test name | Executions per second |
---|---|
foreach | 30.6 Ops/sec |
for | 21.0 Ops/sec |
map | 31.0 Ops/sec |
for no fn call | 22.4 Ops/sec |
I'll break down the provided benchmark definitions and explain what's being tested, along with the pros and cons of each approach.
Benchmark Definition Overview
The main goal of this benchmark is to compare the performance of four different ways to iterate over an array in JavaScript:
forEach
for
loopmap
for
loop without a function call (i.e., just printing the result directly)Array Preparation
Before running the benchmarks, the script prepares an array of 1000 elements with values ranging from 0 to 999.
Library: None
There are no external libraries used in this benchmark.
Special JavaScript Features or Syntax
None mentioned.
Benchmark Test Cases
Here's a breakdown of each test case:
foreach
:arr.forEach(function (item) {
console.log(someFn(item));
});
This uses the forEach
method to iterate over the array, calling someFn
for each element.
Pros: Easy to read and maintain; concise. Cons: May have performance overhead due to function call overhead.
for
loop:for (var i = 0, len = arr.length; i < len; i++) {
console.log(someFn(arr[i]));
}
This uses a traditional for
loop to iterate over the array, calling someFn
for each element.
Pros: Fast and efficient; can be optimized with caching.
Cons: More verbose than forEach
.
map
:arr.map(item => console.log(someFn(item)))
This uses the map
method to create a new array with the results of applying someFn
to each element.
Pros: Concise and expressive; can be optimized with caching. Cons: May have performance overhead due to function call overhead.
for
loop without a function call:for (var i = 0, len = arr.length; i < len; i++) {
console.log(i * 3 * 8);
}
This uses a traditional for
loop to iterate over the array, printing the result directly.
Pros: Fast and efficient.
Cons: More verbose than forEach
.
Benchmark Results
The latest benchmark results show that:
map
is the fastest (30.975736618041992 executions per second)foreach
is slower but still relatively fast (30.572250366210938 executions per second)for
loop without a function call is faster than traditional for
loop with a function call (22.446043014526367 vs 20.981002807617188 executions per second)Other Alternatives
If you wanted to test other approaches, here are some additional options:
reduce
Array.prototype.every()
or Array.prototype.some()
However, these alternatives may not be relevant for this specific benchmark, which is focused on comparing the performance of iterating over an array in JavaScript.