Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test looping
(version: 0)
Comparing performance of:
forEach vs for of vs for
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
forEach
const arr = new Array( 100 ).fill( 0 ).map( (v,i) => i+ 1 ); let t = 0; arr.forEach( v => t += v ); console.log( t );
for of
const arr = new Array( 100 ).fill( 0 ).map( (v,i) => i+ 1 ); let t = 0; for( const v of arr ) { t += v; } console.log( t );
for
const arr = new Array( 100 ).fill( 0 ).map( (v,i) => i+ 1 ); let t = 0; for( let i = 0, num = arr.length; i < num; ++i ) { t += arr[i]; } console.log( t );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach
for of
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 hours ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
654761.2 Ops/sec
for of
716067.4 Ops/sec
for
711929.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition, which is essentially a template for creating and running JavaScript microbenchmarks. In this case, there is no "Script Preparation Code" or "Html Preparation Code", suggesting that the script itself contains the logic being tested. **Options Compared** In this benchmark, three different looping constructs are compared: 1. **`forEach()`**: This method iterates over an array using a callback function. 2. **`for...of`**: This loop construct is used to iterate over arrays and other iterable objects. 3. **Traditional `for` loop**: This is a more manual way of iterating over an array. **Pros and Cons** * **`forEach()`**: This method is concise and easy to read, but it may have performance overhead due to the callback function. It's also not as flexible as traditional loops since you can't easily access the index variable. * **`for...of`**: This loop construct provides a more modern way of iterating over arrays and is generally faster than `forEach()`. However, it requires modern JavaScript engines to support this syntax, which might not be the case for older browsers or environments. * **Traditional `for` loop**: This method gives you fine-grained control over the iteration process but can lead to more verbose code. **Library: `map()`** In the benchmark script, `map()` is used to create an array with a specific value. `map()` returns a new array with the results of applying a provided function on every element in the calling array. In this case, it's used to populate the `arr` array with values. **Special JavaScript Feature: None** There are no special JavaScript features or syntax used in these loops that would affect their performance significantly. **Other Alternatives** If you need to benchmark other looping constructs like `while` loops or `do...while` loops, you can easily create additional test cases by modifying the script accordingly. MeasureThat.net supports a wide range of testing scenarios and allows users to compare various approaches and optimize code for better performance. In conclusion, this benchmark on MeasureThat.net aims to determine which looping construct (forEach, for-of, or traditional for loop) is most efficient in terms of execution time. By comparing these different methods, developers can gain insights into the performance characteristics of each approach and potentially optimize their code accordingly.
Related benchmarks:
reduce vs loop
One For Loop vs Multiple For Loop
forEach vs for loop
loops-forin-forof-native-4
While vs For Loop - Luigi 3
Comments
Confirm delete:
Do you really want to delete benchmark?