Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop vs <Array>.forEach() vs for...of loop
(version: 0)
Comparing performance of:
<Array>.forEach() vs For loop vs For...of loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array(50000, (_, i) => i + 1);
Tests:
<Array>.forEach()
array.forEach(v => v);
For loop
for (let i = 0; i < array.length; i++) array[i];
For...of loop
for (let v of array) v;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
<Array>.forEach()
For loop
For...of loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
<Array>.forEach()
52903912.0 Ops/sec
For loop
38725804.0 Ops/sec
For...of loop
11485117.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `array.forEach(v => v);`: This uses the `.forEach()` method on an array, which executes a callback function for each element in the array. 2. `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. 3. `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 arrays * Traditional `for` loops * `for...of` loops **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. `.forEach()` method: * Pros: concise, easy to read, and maintainable. * Cons: may not be as efficient as traditional loops for large arrays due to additional overhead. 2. Traditional `for` loop: * Pros: control over indexing, cache-friendliness, and can be optimized for specific use cases. * Cons: more verbose, harder to read and maintain, especially for complex logic. 3. `for...of` loop: * Pros: concise, easy to read, and maintainable (similar to `.forEach()`). * Cons: may not be as efficient as traditional loops due to the overhead of creating a new variable scope. **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.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for..of
foreach vs for...of
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?