Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iterate Array for vs for..of vs forEach
(version: 0)
Comparing performance of:
For Loop vs For..of Loop vs forEach Loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(100000).fill(1).map((_, i) => i);
Tests:
For Loop
var total = 0; for(var i = 0, len = arr.length; i < len; i++) total += arr[i];
For..of Loop
var total = 0; for(var val of arr) total += arr[i];
forEach Loop
var total = 0; arr.forEach(val => total += val);
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 provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance of three different approaches to iterate over an array: `for` loop, `for...of` loop, and `forEach` method. **Script Preparation Code** The script preparation code creates a large array (`arr`) with 100,000 elements, all initialized to 1. This array will be used as the input for the benchmarking tests. ```javascript var arr = new Array(100000).fill(1).map((_, i) => i); ``` **Benchmark Definition** The benchmark definition is a JSON object that contains three individual test cases: 1. **For Loop**: `for(var i = 0, len = arr.length; i < len; i++) total += arr[i];` 2. **For...of Loop**: `var total = 0;\r\nfor(var val of arr) total += val;` 3. **forEach Loop**: `var total = 0;\r\narr.forEach(val => total += val);` Each test case has a unique name and benchmark definition. **Comparison Options** The comparison options being tested are: 1. **For Loop**: Uses an explicit index (`i`) to iterate over the array elements. 2. **For...of Loop**: Uses the `for...of` syntax to iterate over the array elements, which is a more concise and modern way of iterating arrays in JavaScript. 3. **forEach Loop**: Uses the `forEach` method to iterate over the array elements. **Pros and Cons** Here are some pros and cons for each comparison option: 1. **For Loop**: * Pros: Can be easily optimized with techniques like loop unrolling or using a loop counter. * Cons: More verbose and requires manual indexing, which can lead to errors if not done correctly. 2. **For...of Loop**: * Pros: More concise and readable than the traditional `for` loop. It's also more modern and widely adopted. * Cons: Can be slower due to the overhead of creating an iterator object and using it for iteration. 3. **forEach Loop**: * Pros: Most concise and easiest to read among all three options. It's also relatively fast since the `forEach` method is optimized by the engine. * Cons: Less control over the iteration process compared to the other two options. **Library Used** None of these tests use any external libraries, but they do utilize the built-in `Array.prototype.forEach()` method for the `forEach Loop`. **Special JS Feature/Syntax** None of these tests use special JavaScript features or syntax like async/await, promises, or modern ECMAScript features (e.g., destructuring, arrow functions). **Other Alternatives** If you're interested in exploring other alternatives, here are a few more options that could be tested: * Using `map()` to create a new array and iterate over it. * Using a `while` loop instead of a traditional `for` loop. * Using `Array.prototype.reduce()` to accumulate values in an array. Keep in mind that the performance differences between these alternatives may not be significant, and the results might depend on the specific use case or environment.
Related benchmarks:
Array fill foreach, vs for i loop
map vs forEach Chris
for vs foreach vs map 2
Array fill map, vs for i loop
Array fill map, vs while loop
Comments
Confirm delete:
Do you really want to delete benchmark?