Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop vs For...of loop
(version: 0)
Comparing performance of:
For loop vs For...of loop
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
For loop
let operation = [1,2,3,4,5,6,7,8,9,10]; for (let i=0; i<operation.length; i++) { console.log(operation[i]); }
For...of loop
let operation = [1,2,3,4,5,6,7,8,9,10]; for(let value of operation) { console.log(value); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop
For...of loop
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/135.0.0.0 Safari/537.36 Edg/135.0.0.0
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For loop
47086.0 Ops/sec
For...of loop
45340.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark measures the performance difference between using traditional `for` loops and `for...of` loops in JavaScript for iterating over an array. **Options Compared** Two options are being compared: 1. **Traditional For Loop**: A classic `for` loop that uses a counter variable (`i`) to iterate over the array indices. 2. **For-of Loop**: An iterable-based loop that uses the spread operator (`...`) or the `for...of` syntax to iterate directly over the elements of an array. **Pros and Cons** 1. **Traditional For Loop** * Pros: + Well-established and widely supported. + Provides more control over iteration, such as incrementing a counter variable. * Cons: + Can be slower due to the overhead of updating a counter variable. 2. **For-of Loop** * Pros: + Faster and more efficient since it avoids the overhead of indexing and incrementing a counter variable. + More concise and readable code, as it allows direct iteration over elements. **Library** None in this case, but if we were to extend this benchmark, we might consider comparing performance with different array implementations (e.g., `Array.from()`, `Array.prototype.slice()`, etc.). **Special JS Feature/Syntax** The benchmark uses the spread operator (`...`) in the `For-of Loop` variant, which is a modern JavaScript feature that allows iterable objects to be expanded into arrays. **Other Considerations** When choosing between traditional `for` loops and `for-of` loops, consider the trade-off between control and performance. If you need more fine-grained control over iteration or work with legacy codebases, a traditional `for` loop might be preferable. However, if speed is critical, the more concise and efficient `for-of` loop is likely a better choice. **Alternatives** Other alternatives for iterating over arrays include: 1. **Array.prototype.forEach()**: A synchronous method that calls a callback function on each element of an array. 2. **Array.prototype.map()**, **Array.prototype.filter()**, and **Array.prototype.reduce()**: These methods perform transformations, filtering, or aggregations on arrays, respectively. For more complex iteration scenarios, consider using techniques like: 1. **Generator functions**: Create generators that yield values from an array, allowing for lazy iteration. 2. **Closures**: Use closures to encapsulate iterative logic and iterate over arrays. Keep in mind that each approach has its own trade-offs, and the best choice depends on your specific use case and performance requirements.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for vs for in
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?