Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comparing For vs For of vs ForEach
(version: 0)
Comparing performance of:
For Loop vs For of loop vs ForEach loop
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function GetId(){ return Math.random().toString().slice(2, 10); } window.array = Array.from(new Array(10000)).map(GetId);
Tests:
For Loop
let res = ""; for(let i = 0; i < window.array.length; ++i){ res = window.array[i]; }
For of loop
let res = ""; for(let value of window.array){ res = value; }
ForEach loop
let res = ""; window.array.forEach(v => { res = v; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For Loop
For of loop
ForEach loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Purpose:** The benchmark is designed to compare the performance of three different approaches for iterating over an array in JavaScript: 1. Traditional `for` loop 2. `for...of` loop 3. `forEach()` method **Script Preparation Code:** The script preparation code generates a large array of 10,000 elements with unique IDs using the `GetId()` function. ```javascript function GetId(){ return Math.random().toString().slice(2, 10); } window.array = Array.from(new Array(10000)).map(GetId); ``` **Html Preparation Code:** There is no HTML preparation code provided for this benchmark. **Individual Test Cases:** 1. **For Loop** ```javascript let res = ""; for(let i = 0; i < window.array.length; ++i){ res = window.array[i]; } ``` This test case uses a traditional `for` loop to iterate over the array and concatenate the elements to the result string `res`. 2. **For of Loop** ```javascript let res = ""; for(let value of window.array){ res = value; } ``` This test case uses an `for...of` loop to iterate over the array and assign each element to the result string `res`. 3. **ForEach Loop** ```javascript window.array.forEach(v => { res = v; }); ``` This test case uses the `forEach()` method to iterate over the array and assign each element to the result string `res`. **Library Used:** The benchmark does not use any external libraries. **Special JS Features/Syntax:** * The `for...of` loop is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It provides a more concise and expressive way of iterating over arrays and other iterable objects. * The `forEach()` method is a standard method on arrays in JavaScript, introduced in ECMAScript 2009 (ES5). **Pros and Cons:** 1. **Traditional For Loop** * Pros: + Widely supported and understood by developers + Can be used with any iterable object, not just arrays * Cons: + Can be error-prone due to the need for manual index management + Not as concise or expressive as other options 2. **For of Loop** * Pros: + Concise and expressive syntax + Easy to read and understand + Reduces the risk of errors due to automatic index management * Cons: + Less widely supported than traditional `for` loops (although this is changing with the adoption of ES6) 3. **ForEach Loop** * Pros: + Concise and expressive syntax + Easy to read and understand + Reduces the risk of errors due to automatic index management * Cons: + Less intuitive for some developers who are not familiar with the `forEach()` method **Alternatives:** 1. **Other Iteration Methods:** There are other iteration methods available in JavaScript, such as the `map()`, `filter()`, and `reduce()` methods, which can be used to iterate over arrays. 2. **Other Loops:** Other loop constructs, such as `while` loops or recursive functions, can also be used for iteration. In summary, the benchmark is designed to compare the performance of three different approaches for iterating over an array in JavaScript: traditional `for` loops, `for...of` loops, and `forEach()` methods. The results will help developers understand the trade-offs between these approaches and choose the most efficient solution for their specific use case.
Related benchmarks:
for vs foreach vs some
for vs foreach vs some
for vs foreach vs some vs for..of non-empty array square root
for vs foreach vs some vs for..of non-empty array square root 1000 elements no console
foreach vs for...of
Comments
Confirm delete:
Do you really want to delete benchmark?