var array = Array.from({length: 100});
var t;
for (let i = 0; i < array.length; i++) {
t = array[i];
}
array.forEach(function(v, i) {
t = v;
});
for (var v of array) {
t = v;
}
for (var [i, v] of array.entries()) {
t = v;
}
for (var k in array) {
t = array[k];
}
for (let i = 0,len = array.length; i < len; i++) {
t = array[i];
}
for (let i = array.length - 1; i >= 0; i--) {
t = array[i];
}
let i=0,len = array.length
while(i < len){
t = array[i]
i++
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
for..of | |
for..of over entries | |
for..in | |
for cache length | |
for reverse | |
while |
Test name | Executions per second |
---|---|
for | 89543.0 Ops/sec |
foreach | 224603.5 Ops/sec |
for..of | 226733.9 Ops/sec |
for..of over entries | 212760.2 Ops/sec |
for..in | 112232.5 Ops/sec |
for cache length | 129762.6 Ops/sec |
for reverse | 129815.3 Ops/sec |
while | 129743.4 Ops/sec |
Let's dive into the world of JavaScript loop performance benchmarking.
What is tested?
The provided JSON represents a set of test cases that compare the performance of different looping constructs in JavaScript:
for
loopforeach
(also known as forEach()
or Array.prototype.forEach()
)for..of
loopfor..of over entries
loopfor...in
loopfor cache length
) loopfor reverse
) loopEach test case measures the execution time of a specific looping construct on a pre-allocated array.
Options compared
The benchmark compares the performance of each looping construct, providing insight into which one is the most efficient.
Pros and Cons:
for
loop: This is a basic loop construct that can be slow due to overhead from incrementing an index variable.foreach
: A more modern looping construct that avoids indexing issues.for
.for..of
loop: A concise and expressive way to iterate over arrays.for
, fewer potential errors.for..of over entries
: Similar to the previous one, but uses the .entries()
method to iterate over an array's index and value pairs.for...in
loop: Iterates over an object's properties (not arrays) using property names as indices.Cache length optimization
This construct uses a variable to store the array length, reducing the number of iterations required.
Pros: Can be faster in certain scenarios due to reduced iterations.
Cons: May require additional memory allocation, potentially leading to performance degradation.
Reverse iteration
Iterates over an array in reverse order.
Pros: Interesting edge case for testing, might reveal unexpected behavior.
Cons: Not suitable for most use cases and can lead to performance overhead.
Other considerations
Keep in mind that the benchmark is specific to Chrome 120+ and may not be representative of other JavaScript engines or environments. Additionally, the performance differences between these constructs can vary depending on the specific use case and array size.
Overall, this benchmark provides a comprehensive view of the different looping constructs in JavaScript, helping developers understand which ones are most efficient for their use cases.