Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs while vs for of vs forEach(optimaized
(version: 0)
Comparing performance of:
for i++ vs for i-- vs while i-- vs for of vs forEach
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [] for (var i = 0; i < 1000; i++) arr[i] = i
Tests:
for i++
var l = arr.length for (var i = 0; i < l; 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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for i++
30263.5 Ops/sec
for i--
30372.8 Ops/sec
while i--
29740.0 Ops/sec
for of
2729405.5 Ops/sec
forEach
2744189.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures the performance of different loops in JavaScript: traditional `for`, `while` with post-decrement (`i--`), `for...of` (introduced in ECMAScript 2015), and `forEach`. The test cases aim to iterate over an array of length 1000, accessing each element using a variable `a`. **Test Cases** Each test case has a unique loop construct being compared. Here's a brief explanation: 1. **Traditional `for` loop**: Iterates from index 0 to the length of the array. 2. **Post-decrement `while` loop (`i--`)**: Decrementing the index in each iteration, starting from the length of the array. 3. **`for...of` loop**: Iterates over the elements of an iterable (in this case, an array) using a "foreach" syntax. 4. **`forEach` function**: Calls a callback function for each element in an array. **Comparison and Performance** The benchmark measures the number of executions per second (`ExecutionsPerSecond`) for each loop construct on a Chrome 126 browser on a Mac OS X 10.15.7 device. The results show that: * `for...of` is generally faster than traditional `for` loops. * Post-decrement `while` loops are slower than both traditional `for` and `for...of` loops. **Pros and Cons** Here's a brief summary of the pros and cons for each loop construct: 1. **Traditional `for` loop**: Pros: * Well-established, widely supported syntax. * Easy to understand and use. 2. **Post-decrement `while` loop (`i--`)**: Cons: * Requires careful indexing to avoid off-by-one errors. * Can be slower than other options due to the increment/decrement operation. 3. **`for...of` loop**: Pros: + More modern and efficient syntax. + Encourages a more "iterative" mindset. 4. **`forEach` function**: Pros: + Concise and expressive syntax for iterating over arrays. **Other Alternatives** While not part of the benchmark, other loop constructs in JavaScript include: * `while` loops with pre-decrement (`i--`) or pre-increment (`++i`). * Array methods like `map()`, `filter()`, and `reduce()`. * Generators and iterators for more complex iteration scenarios. Keep in mind that this benchmark focuses on the relative performance of different loop constructs, rather than their suitability for specific use cases.
Related benchmarks:
Array loop vs for of loop vs foreach vs map
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
Comments
Confirm delete:
Do you really want to delete benchmark?