var array = new Array(100000000);
for (var i = 0; i < array.length; i++) {
array[i];
}
array.forEach(function(i) {
array[i];
});
array.some(function(i) {
array[i];
});
for (var i of array) {
array[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
some | |
for..of |
Test name | Executions per second |
---|---|
for | 0.0 Ops/sec |
foreach | 0.6 Ops/sec |
some | 0.6 Ops/sec |
for..of | 0.1 Ops/sec |
I'd be happy to explain what's being tested in the provided benchmark.
Benchmark Overview
The benchmark compares the performance of four different loop constructs:
for
loopforEach
loop (array method)some
loop (array method, used for checking if at least one element matches a condition)for...of
loop (new loop construct introduced in ECMAScript 2015)Loop Constructs and Their Options
Here's an explanation of each loop construct and the options being compared:
for
loop: The traditional for
loop uses an index variable, starting value, and ending value to iterate over an array. In this benchmark, the loop iterates from 0 to the length of the array.forEach
loop: The forEach
loop is an array method that iterates over an array using a callback function. In this benchmark, the loop uses an arrow function to iterate over the array.for
loops, as it avoids incrementing and decrementing index variables.forEach
method.some
loop: The some
loop is an array method that checks if at least one element in the array matches a condition using a callback function. In this benchmark, the loop uses an arrow function to iterate over the array and check if each element matches the condition.for
loops, as it avoids incrementing and decrementing index variables.some
method.for...of
loop: The for...of
loop is a new loop construct introduced in ECMAScript 2015 that allows iterating over arrays using a simple iteration syntax.for
loops, as it avoids incrementing and decrementing index variables.for...of
loop.Other Considerations
The benchmark also considers the following factors:
Library and Special Features
The some
loop uses the Array.prototype.some()
method, which is a built-in JavaScript library function. No special features or syntax are used in this benchmark.
Alternatives
Other alternatives for measuring loop performance could include: