var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
function someFn(i) {
return i * 3 * 8;
}
arr.forEach(someFn)
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
arr.map(someFn)
for (const i of arr) {
someFn(arr[i]);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for | |
map | |
for-of |
Test name | Executions per second |
---|---|
foreach | 160458.2 Ops/sec |
for | 417786.1 Ops/sec |
map | 135587.6 Ops/sec |
for-of | 394257.4 Ops/sec |
What is being tested?
The provided JSON represents a JavaScript microbenchmark that measures the execution time of different loop constructs on an array: forEach
, for
loops, map
, and for-of
loops.
Options compared
The benchmark compares four different approaches:
forEach
method is called on each element of the array, with a callback function that performs the desired operation.for
loop is used to iterate over the elements of the array, with an index variable i
and a length variable len
.map()
method is called on the array, which returns a new array with the results of applying the provided function to each element.for...of
loop is used to iterate over the elements of the array, without an explicit index variable.Pros and Cons
for
loops.Library
There is no library explicitly used in this benchmark. However, the map()
method uses the built-in JavaScript array prototype methods.
Special JS features or syntax
The benchmark does not use any special JavaScript features or syntax other than the four loop constructs being tested.