const arr = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < arr.length; i++){
var number = arr[i];
}
const arr = [1,2,3,4,5,6,7,8,9,10];
for (var n of arr){
var number = n;
}
const arr = [1,2,3,4,5,6,7,8,9,10];
for (var i in arr){
var number = arr[i];
}
const arr = [1,2,3,4,5,6,7,8,9,10];
arr.forEach(n => {
var number = n;
});
const arr = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0, n = arr.length; i < n; i++){
var number = arr[i];
}
const arr = [1,2,3,4,5,6,7,8,9,10];
while(arr.length) {
var number = arr.shift();
}
const arr = [1,2,3,4,5,6,7,8,9,10];
var i = 0;
var n = arr.length;
while(i < n) {
var number = arr[i];
++i;
}
const arr = [1,2,3,4,5,6,7,8,9,10];
var len = arr.length;
while (len--) {
var number = arr[len];
}
const arr = [1,2,3,4,5,6,7,8,9,10];
for (var i = arr.length; i >= 0; --i){
var number = arr[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
for of | |
for in | |
forEach | |
for 2 | |
while with shift | |
while | |
while with length reduction | |
for reverse |
Test name | Executions per second |
---|---|
for | 19339656.0 Ops/sec |
for of | 22831210.0 Ops/sec |
for in | 816927.6 Ops/sec |
forEach | 25731544.0 Ops/sec |
for 2 | 25599286.0 Ops/sec |
while with shift | 2010041.9 Ops/sec |
while | 27336246.0 Ops/sec |
while with length reduction | 20193752.0 Ops/sec |
for reverse | 23216702.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of six different loop constructs: for
, for in
, for each
(also known as forEach
), for 2
, while with shift
, and while
. The test case uses an array of numbers to demonstrate the difference between these loops.
Options being compared
Here's a brief description of each option:
for
loop that initializes the index variable twice, once before and once inside the loop.shift()
to iterate over it.Performance Comparison
The benchmark measures the number of executions per second for each loop construct on a desktop system running Chrome 127. The results show that:
For
and For in
are the slowest loops, likely due to the overhead of accessing array elements using indices or property names.For each
(forEach) is relatively fast, as it uses a closure to iterate over the array, which is optimized by modern JavaScript engines.For 2
is slightly faster than traditional for
, but still slower than forEach
.While with shift
is surprisingly slow, likely due to the overhead of removing elements from the array using shift()
.While with length reduction
is significantly faster than the other loops, as it avoids the overhead of accessing array elements and uses a simpler iteration logic.Conclusion
This benchmark highlights the importance of choosing the right loop construct for your specific use case. For each
(forEach) is often the best choice when you need to iterate over an array, while traditional for
or while with shift
might be better suited for other scenarios. The results also demonstrate the performance benefits of avoiding unnecessary overhead and using optimized iteration logic.