Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of zzz2
(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 < array.length; ++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
109.9 Ops/sec
foreach
2577.4 Ops/sec
for in
219.8 Ops/sec
for..of
13664.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is crucial for understanding how different approaches impact the execution speed of our code. The provided JSON represents four test cases, each designed to compare the performance of different loop iteration methods: **Test Case 1: "for"** * The benchmark definition uses a traditional `for` loop with a `let` variable declaration. * In this test case, we're adding the values from an array to a running total (`s`) inside the loop. **Pros and Cons:** The traditional `for` loop is simple and efficient. However, it can be error-prone if not used correctly (e.g., forgetting to increment the counter). **Test Case 2: "foreach"** * The benchmark definition uses the `forEach` method on an array. * In this test case, we're also adding the values from an array to a running total (`s`) inside the callback function. **Pros and Cons:** The `forEach` method is more concise and readable than traditional loops. However, it can be slower due to the overhead of function calls and callback execution. **Test Case 3: "for in"** * The benchmark definition uses an old-school `for...in` loop with a non-enumerable property iteration (i.e., using `const n in array`). * In this test case, we're adding the values from an array to a running total (`s`) inside the loop. **Pros and Cons:** The `for...in` loop can be problematic due to its loose type semantics and potential issues with property iteration order. However, it's often considered more "JavaScript-y" than traditional loops. **Test Case 4: "for..of"** * The benchmark definition uses an improved `for...of` loop, which is a modern syntax for iterating over arrays. * In this test case, we're adding the values from an array to a running total (`s`) inside the loop. **Pros and Cons:** The `for...of` loop is more readable and expressive than traditional loops. However, it's not supported in older JavaScript environments (e.g., IE 11). Now, let's discuss some special considerations: * **let and var**: Both declarations can be used to declare variables in the `for` loop. However, `let` is generally preferred due to its block scope. * **Array methods**: The `forEach`, `for...in`, and `for...of` loops all rely on array methods or built-in iterator protocols. Other alternatives for iterating over arrays include: * **Array.prototype.forEach.call()**: Similar to the `forEach` method, but requires calling `call()` explicitly. * **Array.prototype.map()`, **Array.prototype.filter()**, or **Array.prototype.reduce()**: These methods can be used to iterate over an array and perform transformations or accumulate values. In general, when choosing a loop iteration approach, consider factors such as: * Readability: How easy is the code to understand? * Performance: What's the execution speed of each approach? * Compatibility: Will the code work across older JavaScript environments? By understanding these considerations, developers can make informed decisions about how to iterate over arrays in their JavaScript code.
Related benchmarks:
foreach vs for..of
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..in vs for..of zzz3
for vs foreach vs for..in vs for..of zzz4
Comments
Confirm delete:
Do you really want to delete benchmark?