var array = []
var map = new Map()
var object = {}
for (var i = 0; i < 2000; i++) {
array[i] = i;
map.set(i.toString(), i);
object[i.toString()] = i;
}
var results = []
array.forEach(i => {
results.push(i * 2)
})
for (const i of array) {
results.push(i * 2)
}
map.forEach(i => {
results.push(i * 2)
})
for (const i in object) {
results.push(object[i] * 2)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.forEach | |
Array for...of | |
Map.forEach | |
object for...in |
Test name | Executions per second |
---|---|
Array.forEach | 4747.5 Ops/sec |
Array for...of | 5720.8 Ops/sec |
Map.forEach | 4872.9 Ops/sec |
object for...in | 2268.4 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of three different iteration approaches in JavaScript: Array.forEach
, Array for...of
, and Map.forEach
vs. Object for...in
. The benchmark creates an array, a Map, and an object with 2000 elements each, and then uses these data structures to iterate over them and perform calculations.
Iteration Approaches
forEach
method of arrays in JavaScript, which iterates over the elements of an array by calling a provided callback function once for each element.for...of
loop with an iterator that can iterate over an array. It provides a more modern and concise way to iterate over arrays compared to traditional forEach
.forEach
method of Maps in JavaScript, which iterates over the key-value pairs of a Map.for...in
loop with an iterator that can iterate over the properties (key-value pairs) of an object.Comparison
The benchmark compares the performance of these four approaches:
for...of
loop is expected to be faster than the traditional forEach
method, as it avoids the overhead of a callback function.forEach
is generally faster than object iteration using for...in
, as it uses a more efficient iterator that can skip over non-enumerable properties.Pros and Cons
for...of
.forEach
.forEach
, skips non-enumerable properties.Library Usage
None of the test cases explicitly use any libraries, but it's worth noting that some JavaScript engines or browsers may have optimized implementations for certain methods (e.g., WebAssembly for performance-critical code).
Special JS Features/Syntax
The benchmark uses the for...of
loop with an iterator, which is a modern JavaScript feature introduced in ECMAScript 2015. This loop provides a more concise way to iterate over arrays and other iterable objects.
Alternatives
Other alternatives for iterating over data structures include:
forEach
: A popular utility library that provides a robust implementation of the forEach
method.forEach
: Another popular utility library that provides a more concise implementation of the forEach
method.for
loops: An alternative approach to iteration that uses traditional for
loops, which can be more verbose but still effective.In summary, the benchmark highlights the performance differences between various iteration approaches in JavaScript and encourages developers to consider the trade-offs between readability, maintainability, and performance when choosing an iteration method.