var array = [];
for (var i=0, t=10000; i<t; i++) {
array.push(Math.round(Math.random() * t))
}
var t=0;
for (let i = 0; i < array.length; i++) {
t += array[i];
}
for (var i = 0; i < array.length; i++) {
t += array[i];
}
let len=array.length;
for (let i = 0; i < len; i++) {
t += array[i];
}
const len=array.length;
for (let i = 0; i < len; i++) {
t += array[i];
}
var len=array.length;
for (var i = 0; i < len; i++) {
t += array[i];
}
array.forEach(function(v, i) {
t += v;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for let | |
for var | |
for let cached | |
for let cached (const) | |
for var cached | |
forEach |
Test name | Executions per second |
---|---|
for let | 604.2 Ops/sec |
for var | 603.6 Ops/sec |
for let cached | 779.6 Ops/sec |
for let cached (const) | 818.5 Ops/sec |
for var cached | 790.5 Ops/sec |
forEach | 1101.9 Ops/sec |
Let's break down the provided benchmark data.
Benchmark Purpose
The benchmark measures the performance of different JavaScript iteration methods in executing a simple calculation on an array of numbers. The goal is to determine which method is the fastest.
Iteration Methods Compared
The benchmark compares six different iteration methods:
for
loop with the let
keyword for variable declaration.for
loop with the var
keyword for variable declaration.for let
, but uses a constant variable declaration (const
) instead of re-declaration on each iteration.Array.prototype.forEach()
method, which calls a callback function once for each element in the array.Pros and Cons of Each Approach
const
vs let
.Library Usage
There is no explicit library usage mentioned in the benchmark definition or test cases.
Special JavaScript Features or Syntax
No special features or syntax are used beyond what's already described above. However, it's worth noting that forEach
method uses a callback function, which may incur additional overhead compared to traditional loops.
Alternative Benchmarks
Other alternatives for benchmarking iteration performance could include: