var array = new Array(100);
for (var i = 0; i < array.length; i++) {
array[i];
}
array.forEach(function(i) {
i;
});
for (var i = 0,n = array.length; i < n; i++) {
array[i];
}
for (var i of array) {
i;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
for cached | |
for..of |
Test name | Executions per second |
---|---|
for | 61270.1 Ops/sec |
foreach | 3017946.2 Ops/sec |
for cached | 122613.7 Ops/sec |
for..of | 5837318.0 Ops/sec |
The provided JSON represents a JavaScript microbenchmark test case for comparing the performance of different loop iteration methods: for
, foreach
, for cached
, and for..of
.
Loop Iteration Methods
for
loop uses an explicit counter variable (i
) to iterate over the array. This method requires manual incrementing of the counter variable.forEach
method is a part of the ECMAScript 2015 (ES6) standard and allows iterating over arrays using a callback function. It iterates over each element in the array, but does not provide direct access to the index or length of the array.for
loop uses two counter variables: one for the iteration (i
) and another for the array length (n
). This method is similar to the traditional for
loop but avoids re-calculating the array length on each iteration.for..of
loop is a part of ES6 and allows iterating over arrays using a direct access to each element without the need for an index variable.Comparison and Performance
The provided benchmark test case compares the performance of these four loop iteration methods:
for
loops.Library and Special Features
There are no libraries used in this benchmark test case. However, the ECMAScript 2015 (ES6) standard is utilized for the forEach
and for..of
loop methods.
Other Considerations
When working with loops in JavaScript:
forEach
and for..of
, which are designed for efficient array iteration.Alternatives
For similar benchmark test cases:
Keep in mind that different benchmarks may prioritize specific aspects of JavaScript performance.