Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of vs for vs forEach
(version: 0)
Comparing performance of:
for vs for..of vs forEach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(200);
Tests:
for
for (let i = 0; i < arr.length; ++i) { arr[i]; }
for..of
for (const el of arr) { el; }
forEach
arr.forEach(el => { el; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
for..of
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
3324882.0 Ops/sec
for..of
18459096.0 Ops/sec
forEach
7389846.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark definition is a JSON object that contains three test cases: 1. `for` 2. `for..of` 3. `forEach` Each test case represents a different way to iterate over an array in JavaScript. **Script Preparation Code** The script preparation code creates an array `arr` with 200 elements and assigns it to the variable `arr`. This array will be used as input for each test case. **Html Preparation Code** There is no HTML preparation code, which means that this benchmark does not test anything related to the DOM or HTML rendering. **Test Cases** The three test cases are: 1. **`for`**: This test case uses a traditional `for` loop to iterate over the array. The loop variable `i` is incremented manually using the `++i` expression. 2. **`for..of`**: This test case uses the `for...of` loop, which was introduced in ECMAScript 2015 (ES6). It allows iterating directly over the elements of an iterable object, such as an array. 3. **`forEach`**: This test case uses the `forEach()` method, which is a built-in method of arrays. It calls a provided callback function once for each element in the array. **Options Compared** The benchmark compares three different ways to iterate over an array: 1. Traditional `for` loop 2. `for...of` loop 3. `forEach()` method These options differ in their syntax, performance, and potential use cases. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each approach: * **Traditional `for` loop**: This is the most widely supported and familiar way to iterate over an array. It can be slower than the other two options due to the overhead of manual incrementation. + Pros: Wide support, easy to understand, no external dependencies required + Cons: Potential performance issues with manual incrementation * **`for...of` loop**: This loop is more concise and expressive than traditional `for` loops. It's also more efficient, as the iterator object handles incrementation internally. + Pros: More readable and maintainable code, faster execution time + Cons: Requires ECMAScript 2015 (ES6) or later support * **`forEach()` method**: This is a convenient way to iterate over an array without manually handling indices. However, it may have performance implications due to the overhead of function calls. + Pros: Easy to use, no manual indexing required + Cons: Potential performance issues with function call overhead **Library and Special JS Feature** There are no libraries used in this benchmark. Additionally, there is no special JavaScript feature or syntax mentioned. **Other Alternatives** If you're looking for alternative ways to iterate over an array, here are a few more options: * **`while` loop**: Another traditional looping construct that can be used with arrays. * **Array.prototype.reduce()` and `Array.prototype.map()`: These methods allow for functional programming styles of iteration, but may require additional code and dependencies. Overall, this benchmark provides a good introduction to the different ways JavaScript iterates over arrays, highlighting the trade-offs between simplicity, readability, and performance.
Related benchmarks:
Iteration through array; of vs forEach
for (i < n) vs forEach vs for...of
foreach vs for...of
for of vs forEach with console log
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?