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++;
}
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40));
items.reduce((acc,value) => {
acc.push(value);
return acc;
}, new Array());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for-in | |
for-of | |
for | |
optimized-for | |
reduce |
Test name | Executions per second |
---|---|
foreach | 5260.1 Ops/sec |
for-in | 5135.1 Ops/sec |
for-of | 5464.9 Ops/sec |
for | 5541.6 Ops/sec |
optimized-for | 5567.5 Ops/sec |
reduce | 5309.9 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark test case hosted on MeasureThat.net. The test compares the execution performance of six different approaches to iterate over an array: foreach
, for-in
, for-of
, for
, optimized-for
, and reduce
.
Options Compared
foreach
: Iterates over an array using a traditional for loop.for-in
: Iterates over an array using the in
operator, which returns a property name-value pair object.for-of
: Iterates over an array using the of
operator, which is a new way to iterate over arrays introduced in ECMAScript 2015 (ES6).for
: A traditional for loop without an iterator or index variable.optimized-for
: An optimized for loop that iterates over an array by accessing its length
property directly, reducing the number of iterations.reduce
: Uses the Array.prototype.reduce()
method to accumulate values in an array.Pros and Cons
foreach
: Simple and straightforward, but may be slower due to the overhead of a traditional for loop.for-in
: May be slower due to the unnecessary iteration over property names, which can lead to unnecessary computations.for-of
: A modern and efficient way to iterate over arrays, with minimal overhead.for
: A simple and straightforward approach, but may be slower than optimized-for
.optimized-for
: Optimized for performance, as it reduces the number of iterations by accessing length
directly. However, it may be less readable due to its concise syntax.reduce
: A useful method for accumulating values in an array, but may incur additional overhead due to the function invocation.Library and Purpose
None of the test cases use a library beyond standard JavaScript. The Array.prototype.reduce()
method is used in one of the test cases, which is a built-in method provided by the ECMAScript standard.
Special JS Feature or Syntax
The of
operator ( introduced in ES6) is used in two test cases (for-of
) to iterate over an array. This is a new syntax introduced in modern JavaScript versions.
Other Alternatives
For iterating over arrays, other alternatives include:
forEach()
with a callback functionmap()
, filter()
, and every()
methods for specific use casesHowever, the original six approaches listed in the benchmark (foreach, for-in, for-of, for, optimized-for, and reduce) are commonly used and widely supported across different JavaScript implementations.