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 > 0; i--) {
someFn(arr[i]);
}
var len = arr.length;
var i = 0;
while(i < len){
someFn(arr[i]);
i++;
}
var i = arr.length - 1;
while(i > 0){
someFn(arr[i]);
i--;
}
arr.map(i => someFn(i))
arr.forEach(i => someFn(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-- | |
map | |
forEach |
Test name | Executions per second |
---|---|
For loop, basic | 2134.0 Ops/sec |
While loop, basic | 2198.2 Ops/sec |
For loop, cached (inside for statement) | 2927.5 Ops/sec |
For loop, cached (outside for statement) | 3325.2 Ops/sec |
For loop, i-- (caching inside for loop) | 3298.9 Ops/sec |
For loop, i-- (caching outside for loop) | 3220.6 Ops/sec |
While loop, cached | 2067.5 Ops/sec |
While loop, i-- | 2114.1 Ops/sec |
map | 3484.8 Ops/sec |
forEach | 3860.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is tested on the provided JSON?
The provided JSON represents a set of test cases for measuring the performance of different loops and array operations in JavaScript.
Each test case consists of a Benchmark Definition
string that describes the loop or array operation to be tested. The test cases cover various scenarios, including:
Options compared
The benchmark compares the performance of each test case across different browsers and devices.
Pros and cons of different approaches:
Here's a brief overview of the pros and cons of each approach:
Other observations:
In summary, the provided JSON represents a comprehensive set of tests for measuring the performance of JavaScript loops and array operations. By analyzing the results, developers can gain insights into the trade-offs between different approaches and optimize their code for better performance across various scenarios.