Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of zzz3
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100000); for (let i = 0; i < 100000; ++i) { array[i] = i; }
Tests:
for
let s = 0; for (var i = 0; i < array.length; i++) { s+=array[i]; }
foreach
let s = 0; array.forEach(function(n) { s+=n; });
for in
let s = 0; for (const n in array) { s+=n; }
for..of
let s = 0; for (const n of array) { s+=n; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
111.8 Ops/sec
foreach
2601.6 Ops/sec
for in
219.6 Ops/sec
for..of
12949.4 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 benchmark definition provided in JSON represents a test case that compares the performance of four different looping approaches: `for`, `foreach`, `for...in`, and `for...of`. The script preparation code is identical for all four tests, which initializes an array with 100,000 elements using the `Array` constructor. **Looping Approaches** Here's a brief overview of each looping approach: 1. **For**: This traditional looping approach uses a counter variable to iterate over the array. 2. **Foreach**: This approach uses the `forEach` method to iterate over the array, which is similar to a `for` loop but more concise. 3. **For...in**: This approach uses the `for...in` statement to iterate over the properties of an object (in this case, the array). 4. **For...of**: This approach uses the `for...of` statement to iterate over the elements of an array. **Comparison and Pros/Cons** Here's a comparison of the four looping approaches: * **For**: This approach is the most traditional and straightforward way to loop through an array. It's easy to understand and optimize. * Pros: Easy to read, maintain, and debug. * Cons: Can be slower than other methods due to the overhead of incrementing the counter variable. * **Foreach**: This approach is concise and eliminates the need for explicit looping variables. * Pros: More concise, easier to write, and potentially faster due to reduced overhead. * Cons: Less readable and maintainable compared to traditional `for` loops, as it relies on the behavior of the callback function. * **For...in**: This approach iterates over the properties (not indices) of an array. It's not suitable for this specific use case. * Pros: None in this context, as it doesn't directly address the problem. * Cons: Not designed for arrays; instead, use `for...of`. * **For...of**: This approach is similar to `foreach`, but it explicitly iterates over the elements of an array using a `const` loop variable. * Pros: More readable and maintainable compared to traditional `forEach`, as it clearly indicates the intent. * Cons: Can be slower due to the overhead of incrementing the `const` loop variable. **Library and Special JS Features** There are no libraries explicitly mentioned in the benchmark definition. However, some special JavaScript features worth noting: * **let`, `const`, and `var`: These keyword declarations allow for block scope, which helps prevent global variables from being created. * **Arrow functions**: The callback function used with `foreach` is an arrow function, which eliminates the need to declare a separate function variable. **Benchmark Results** The latest benchmark results show that: 1. **For...of** has the highest performance (12949.4453125 executions per second). 2. **Foreach** follows closely behind (2601.635498046875 executions per second). 3. **For in** is slower than both `for` and `foreach` (219.63551330566406 and 111.8130111694336 executions per second, respectively). Keep in mind that these results are specific to this particular benchmark definition and may not generalize across all scenarios. **Other Alternatives** Some alternative looping approaches you might consider: * **Array.prototype.reduce()**: This method accumulates values by applying a callback function to each element. * **Map()**: Similar to `reduce`, but returns a new Map object instead of accumulating the result in a single value.
Related benchmarks:
foreach vs for..of
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..in vs for..of zzz2
for vs foreach vs for..in vs for..of zzz4
Comments
Confirm delete:
Do you really want to delete benchmark?