Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs for of vs forEach
(version: 0)
Comparing performance of:
for loop vs forEach vs for of vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100000);
Tests:
for loop
let value; for (var i = 0; i < array.length; i++) { value = array[i]; }
forEach
let value; array.forEach(function(i) { value = i; });
for of
let value; for (var i of array) { value = i; }
reduce
const value = array.reduce((prev, current) => current, null)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for loop
forEach
for of
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 146 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
3687.3 Ops/sec
forEach
5610.7 Ops/sec
for of
27996.9 Ops/sec
reduce
4679.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of three different loops in JavaScript: `for`, `for...of`, and `forEach`. The script preparation code creates an array of 100,000 elements. The benchmark is designed to test which loop executes faster. **Options Compared** The options being compared are: * `for`: a traditional `for` loop that uses a counter variable (`i`) to iterate over the array. * `for...of`: a newer `for` loop syntax that uses an iterator expression (`array[i]`) to iterate over the array. * `forEach`: a method call on the array that iterates over its elements using a callback function. **Pros and Cons** 1. **For Loop** * Pros: widely supported, easy to understand and implement. * Cons: can be slower than newer loop syntaxes due to the overhead of incrementing the counter variable. 2. **For...of Loop** * Pros: more concise and expressive, eliminates the need for explicit counter variables. * Cons: may have performance implications due to the use of iterator expressions. 3. **forEach Loop** * Pros: concise and easy to read, eliminates the need for explicit loops. * Cons: may not be as efficient as traditional `for` or `for...of` loops. **Library and Purpose** None of the test cases explicitly use a library or third-party function. The performance tests are designed to measure the native JavaScript engine's performance. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in these benchmarks. All three loop options are standard features of the language. **Alternative Loop Options** Other alternative loop options that could be included in a benchmark like this include: * `while`: a traditional `while` loop that iterates over the array. * `map()`: a method call on the array that creates a new array with transformed elements. * `filter()`: a method call on the array that creates a new array with filtered elements. These alternative options could be included to provide a more comprehensive understanding of the performance characteristics of different JavaScript loops and iteration methods.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?