Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs while vs for of vs forEach
(version: 4)
Comparing performance of:
for i++ vs for i-- vs while i-- vs for of vs forEach
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [] for (var i = 0; i < 1000; i++) arr[i] = i
Tests:
for i++
for (var i = 0; i < arr.length; i++) { var a = arr[i] }
for i--
for (var i = arr.length; i >= 0; i--) { var a = arr[i] }
while i--
var i = arr.length while (i--) { var a = arr[i] }
for of
for (var v of arr) { var a = v }
forEach
arr.forEach(v => { var a = v })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for i++
for i--
while i--
for of
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for i++
4167169.2 Ops/sec
for i--
4173808.5 Ops/sec
while i--
3780903.8 Ops/sec
for of
1757920.2 Ops/sec
forEach
2499544.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark tests different loop constructs in JavaScript: traditional `for` loops, `while` loops, and array iteration methods like `forEach`. **Loop Constructs Comparison** 1. **Traditional `for` loop (`for i++`)**: * Pros: Easy to read and write, allows for manual index management. * Cons: Can be slower due to overhead of incrementing the index variable. 2. **Decreasing `for` loop (`for (var i = arr.length; i >= 0; i--)`)**: * Pros: Similar to traditional `for` loop but with a more concise syntax, can be faster for large arrays. * Cons: May have issues with negative indices and can be less readable. 3. **Traditional `while` loop (`while i--`)**: * Pros: Allows for manual index management, can be faster than traditional `for` loops. * Cons: Can be slower due to the overhead of incrementing the index variable inside the loop. 4. **Array iteration method with `of` (`for (var v of arr) { ... }`)**: * Pros: Fast and efficient, easy to read and write, reduces overhead compared to traditional loops. * Cons: Only supported in modern JavaScript engines and may not be compatible with older browsers. 5. **`forEach` method (`arr.forEach(v => { ... })`)**: * Pros: Easy to use, fast, and efficient, and is a built-in array method that reduces boilerplate code. * Cons: Requires an array as input, which can lead to issues if the input array changes size during execution. **Other Considerations** * The `for i++` loop uses post-increment (`i++`) instead of pre-increment (`++i`). Post-increment is generally faster because it doesn't involve incrementing the variable before using it. * The `forEach` method is designed specifically for array iteration and is typically faster than traditional loops. **Library and Special JS Features** None mentioned in this specific benchmark. **Alternative Loop Constructs** Other alternative loop constructs that could be tested include: * `for...in`: A loop that iterates over the properties of an object. * `let` or `const` variables with `for` loops: Using `let` or `const` variables instead of `var` can improve performance by avoiding variable hoisting. Keep in mind that the choice of loop construct depends on the specific use case and the requirements of the application. The benchmark results provided may not be representative of all scenarios, and different loop constructs may perform better under different conditions.
Related benchmarks:
index loop vs for-of loop vs foreach vs map
Array loop vs for of loop vs foreach vs map (2)
Array loop: forEach vs for vs map vs for of entries
for (i < n) vs forEach vs for...of
Fastest iteration over array: map vs forEeach vs while vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?