var array = Array(50000, (_, i) => i + 1);
array.forEach(v => v);
for (let i = 0; i < array.length; i++) array[i];
for (let v of array) v;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
<Array>.forEach() | |
For loop | |
For...of loop |
Test name | Executions per second |
---|---|
<Array>.forEach() | 103255488.0 Ops/sec |
For loop | 71831752.0 Ops/sec |
For...of loop | 102817288.0 Ops/sec |
Let's break down the provided JSON benchmark definition and test cases.
Benchmark Definition
The benchmark is designed to compare the performance of three different approaches:
array.forEach(v => v);
: This uses the .forEach()
method on an array, which executes a callback function for each element in the array.for (let i = 0; i < array.length; i++) array[i];
: This uses a traditional for
loop to iterate over the elements of the array and access them by index.for (let v of array) v;
: This uses the for...of
loop, which iterates over an iterable object (like an array) and assigns each element to a variable.Options Compared
The benchmark is comparing the performance of these three approaches:
.forEach()
method on arraysfor
loopsfor...of
loopsPros and Cons
Here's a brief summary of the pros and cons of each approach:
.forEach()
method:for
loop:for...of
loop:.forEach()
).Library Usage
There is no explicit library usage in this benchmark definition. However, Array
and its methods are part of the JavaScript standard library.
Special JS Features or Syntax
The only special feature used here is the .forEach()
method on arrays, which is a built-in method provided by the JavaScript standard library (ECMAScript 2015). The other two approaches do not rely on any specialized features.