window.arr = [1, "two", 3, "four", 5, {"sixth": 6}, 7, 8, 9, "ten"];
for (let i = 0; i < arr.length; i++) console.log(arr[i]);
arr.forEach(el => console.log(el));
arr.map(el => console.log(el));
let i = 0;
while (i < arr.length) { console.log(arr[i]); i++; }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for loop | |
forEach | |
map | |
while |
Test name | Executions per second |
---|---|
for loop | 19064.9 Ops/sec |
forEach | 17749.4 Ops/sec |
map | 18171.6 Ops/sec |
while | 16112.5 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark, specifically measuring the performance of different iteration methods over an array: map
, forEach
, for
loop, and while
. The benchmark is designed to compare the execution speeds of these four approaches in Firefox 115 on a Windows desktop.
Options Compared
map()
method to create a new array with the results of applying a provided function to each element of the original array.forEach()
method to iterate over the array and execute a provided function for each element.for
loop to iterate over the array, accessing each element using an index variable.while
loop to iterate over the array, incrementing an index variable until it reaches the length of the array.Pros and Cons
map
or forEach
.map
and forEach
.Library Usage
None of the provided benchmark definitions explicitly use a library. However, it's worth noting that map
and forEach
rely on the Array prototype methods, which are part of the ECMAScript standard.
Special JS Features or Syntax
None of the provided benchmark definitions explicitly use any special JavaScript features or syntax beyond what is required for basic iteration.
Other Alternatives
For similar performance benchmarks:
reduce()
, every()
, or some()
for array iteration, which might provide better performance in certain cases.Keep in mind that the specific results of this benchmark may vary depending on the version of JavaScript, browser, and platform used.