var iterations = 10000;
for (i = 0; i < iterations; i++) {
console.log(i);
}
Array(iterations).fill('').forEach((value, index) => console.log(index));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
fill and foreach |
Test name | Executions per second |
---|---|
for | 50.5 Ops/sec |
fill and foreach | 58.1 Ops/sec |
Let's dive into the world of MeasureThat.net!
Benchmark Definition:
The benchmark is designed to compare two approaches for filling an array and then iterating over it using foreach
. The goal is to determine which approach is faster.
Options Compared:
i
) until the desired condition is met.Array.fill()
to create an array and then iterating over it using forEach
, logging each element's index.Pros and Cons:
forEach
is optimized for iterating over arrays, and fill()
creates an array in a single operation.Array.fill()
and forEach()
, which might be unfamiliar to some developers.Library and Purpose:
Special JS Features/Syntax:
None mentioned in this specific benchmark.
Other Alternatives:
In addition to for
loops and Array.fill()
with forEach
, other approaches might include:
while
loop instead of a for
looprequestAnimationFrame()
or setTimeout()
Benchmark Results:
The latest benchmark results show that the Array.fill()
with forEach
approach is faster, executing at 58.05 executions per second on a Chrome 120 browser on a Mac OS X 10.15.7 device. The traditional for
loop approach executes at 50.54 executions per second.
Keep in mind that these results are specific to the MeasureThat.net benchmark and may not reflect your individual experience or code optimizations.