const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
let index = 0;
items.forEach(i => index++);
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
let index = 0;
for (let i in items) {
index++;
}
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
let index = 0;
for (let i of items) {
index++;
}
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
let index = 0;
for(let i = 0; i < items.length; ++i) {
index++;
}
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
let index = 0;
for(let i = items.length; i > 0; --i) {
index++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for-in | |
for-of | |
for | |
optimized-for |
Test name | Executions per second |
---|---|
foreach | 235276.0 Ops/sec |
for-in | 26673.7 Ops/sec |
for-of | 205696.7 Ops/sec |
for | 252250.8 Ops/sec |
optimized-for | 231390.8 Ops/sec |
I'd be happy to explain the benchmark and its results.
Benchmark Definition
The benchmark is designed to test the performance of different JavaScript loop constructs: forEach
, for-in
, for-of
, and traditional for
loops, as well as an optimized version of the for
loop. The benchmark measures the time taken by each loop construct to iterate over a large array of random numbers.
Options Compared
The benchmark compares four main options:
forEach
: This method calls a provided callback function for each element in an array, without requiring explicit index management.for-in
: This loop construct iterates over the properties (including indices) of an object using the in
operator.for-of
: This is a newer loop construct introduced in ECMAScript 2015, which allows iterating over arrays or iterators without requiring explicit index management.for
loop: This classic loop construct uses a counter variable to iterate over an array.Pros and Cons
Here's a brief summary of the pros and cons of each option:
forEach
: Pros: concise, easy to read, and maintain. Cons: can be slower due to the overhead of function calls and potentially more memory allocations.for-in
: Pros: works on both arrays and objects. Cons: can be slower due to the overhead of accessing properties using in
, and may not work well with arrays that have a large number of numeric indices (which are assumed to be property names).for-of
: Pros: concise, easy to read, and maintain. Cons: requires support for modern JavaScript versions; can be slower due to the overhead of creating an iterator.for
loop: Pros: widely supported, fast, and flexible. Cons: may require more boilerplate code.Optimized for
Loop
The optimized version of the for
loop uses a technique called "iterative increment" instead of incrementing the counter variable using the ++ operator. This optimization can reduce the overhead of incrementing the counter variable, potentially leading to faster performance.
Libraries and Special Features
In this benchmark, none of the test cases use any external libraries or special JavaScript features beyond what's included in the standard ECMAScript specification.
Other Alternatives
For more comprehensive loop performance comparisons, consider using other benchmarks like:
These tools provide a wider range of test cases and can offer more detailed insights into the performance characteristics of different JavaScript loops.