var array = new Array(100);
for (let i = 0; i < array.length; i++) {
array[i] = i % 2;
}
array.forEach(i => {
array[i] = i % 2;
});
array.some(i => {
array[i] = i % 2;
});
for (const i of array) {
array[i] = i % 2;
}
for (let i = 0, l = array.length; i < l; i++) {
array[i] = i % 2;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
some | |
for..of | |
for cached |
Test name | Executions per second |
---|---|
for | 77041.2 Ops/sec |
foreach | 149964.1 Ops/sec |
some | 152050.0 Ops/sec |
for..of | 149718.1 Ops/sec |
for cached | 155528.6 Ops/sec |
I'll provide a detailed explanation of the benchmark, its test cases, and the different approaches compared.
Benchmark Overview
The benchmark aims to compare the performance of different loop constructs in JavaScript: for
, foreach
, some
, and for..of
with caching. The goal is to identify which approach is the most efficient.
Test Cases
Each test case represents a specific loop construct:
for
: A traditional for
loop that iterates over an array using a counter variable.foreach
: A loop that uses the forEach
method, which executes a callback function for each element in the array.some
: A loop that uses the some
method, which returns a boolean value indicating whether at least one element in the array passes a test.for..of
: A loop that uses the for..of
statement, which iterates over an iterable (in this case, an array) using a foreach-like syntax.for cached
: A loop that uses a traditional for
loop with caching, where the counter variable is stored in a separate variable to avoid recalculating it on each iteration.Performance Comparison
The benchmark measures the execution time of each test case on a Chrome 80 browser running on Windows 10. The results are as follows:
for cached
: 155,528.5625 executions per secondsome
: 152,049.984375 executions per secondforeach
: 149,964.078125 executions per secondfor..of
: 149,718.109375 executions per secondfor
: 77,041.2265625 executions per secondThe results indicate that:
for cached
is the fastest approach.some
and foreach
are very close in performance, with foreach
being slightly faster.for..of
is slower than both foreach
and some
.for
is the slowest approach.Pros and Cons
Here's a brief summary of each approach:
for cached
: Pros: avoids recalculating the counter variable on each iteration, which can improve performance. Cons: requires manual caching.foreach
: Pros: concise and easy to read, with good cache locality. Cons: may have slower performance due to method call overhead.some
: Pros: concise and easy to read, with good cache locality. Cons: may have slower performance due to method call overhead.for..of
: Pros: concise and easy to read, with good cache locality. Cons: slower than traditional foreach
or some
.for
: Pros: widely supported and well-understood. Cons: slower performance compared to other approaches.Other Considerations
for cached
approach relies on caching the counter variable, which can lead to incorrect results if the cache is not properly implemented.foreach
and some
methods have additional overhead due to method call and object lookup, which may affect their performance.Alternatives
If you're looking for alternative loop constructs or optimization techniques, consider:
Map.prototype.forEach()
: A more efficient alternative to forEach
, as it avoids the overhead of a separate array.Array.prototype.reduce()
: A concise way to iterate over an array and accumulate values.Set
or Map
data structures: For efficient set operations, such as union, intersection, or difference.Keep in mind that the choice of loop construct ultimately depends on the specific use case and performance requirements.