var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
function someFn(ix) {
return ix * 5 + 1 / 3 * 8;
}
for (var i = 0; i < arr.length; i++) {
someFn(arr[i]);
}
var i = 0;
while (i < arr.length) {
someFn(arr[i]);
i++;
}
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
var len = arr.length;
for (var i = 0; i < len; i++) {
someFn(arr[i]);
}
for (var i = arr.length; i > 0; i--) {
someFn(arr[i]);
}
var i = arr.length;
for (; i--;) {
someFn(arr[i]);
}
var len = arr.length;
var i = 0;
while(i < len){
someFn(arr[i]);
i++;
}
var i = arr.length;
while(i--){
someFn(arr[i]);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For loop, basic | |
While loop, basic | |
For loop, cached (inside for statement) | |
For loop, cached (outside for statement) | |
For loop, i-- (caching inside for loop) | |
For loop, i-- (caching outside for loop) | |
While loop, cached | |
While loop, i-- |
Test name | Executions per second |
---|---|
For loop, basic | 3264.0 Ops/sec |
While loop, basic | 3147.3 Ops/sec |
For loop, cached (inside for statement) | 5092.2 Ops/sec |
For loop, cached (outside for statement) | 5373.7 Ops/sec |
For loop, i-- (caching inside for loop) | 5192.6 Ops/sec |
For loop, i-- (caching outside for loop) | 5287.7 Ops/sec |
While loop, cached | 5155.0 Ops/sec |
While loop, i-- | 5408.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this Loop Optimization benchmark.
Benchmark Overview
The Loop Optimization benchmark measures the performance differences between various for/while loops using the same function, someFn
, which takes an index as input. The test cases are designed to demonstrate the impact of loop optimization techniques on execution speed.
Loop Optimization Techniques
The benchmark tests six different loop optimization approaches:
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
for (var i = arr.length; i > 0; i--) { someFn(arr[i]); }
var len = arr.length; while(i < len){someFn(arr[i]);i++;}
for (var i = 0; i < arr.length; i++) { someFn(arr[i]); }
var i = 0; while(i < arr.length){someFn(arr[i]);i++;}
for (var i = 0; i < len; i--) { someFn(arr[i]); }
Pros and Cons of Each Approach
Here's a brief analysis of each approach:
Performance Results
The benchmark results show that the optimized approaches outperform the basic versions, with the most significant performance gains achieved by using cached array lengths or index increments. The order of performance results is:
Keep in mind that these results are specific to this benchmark and may vary depending on other factors, such as hardware, software configurations, or JavaScript engine optimizations.
In conclusion, this Loop Optimization benchmark highlights the importance of optimizing loops in JavaScript code, particularly when it comes to caching array lengths or index increments. By applying simple yet effective techniques, developers can significantly improve the performance of their applications.