Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Looping over an array
(version: 0)
Compare loop performance
Comparing performance of:
for var vs for let vs for var cached length vs for let cached length vs forEach function vs forEach arrow vs some function vs some arrow vs for...of var vs for...of const
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100); for (var i = 0; i < 100; ++i) array[i] = i;
Tests:
for var
for (var i = 0; i < array.length; ++i) { array[i] = i % 2; }
for let
for (let i = 0; i < array.length; ++i) { array[i] = i % 2; }
for var cached length
for (var i = 0, l = array.length; i < l; ++i) { array[i] = i % 2; }
for let cached length
for (let i = 0, l = array.length; i < l; ++i) { array[i] = i % 2; }
forEach function
array.forEach(function(v, i) { array[i] = i % 2; });
forEach arrow
array.forEach((v, i) => { array[i] = i % 2; });
some function
array.some(function(v, i) { array[i] = i % 2; });
some arrow
array.some((v, i) => { array[i] = i % 2; });
for...of var
for (var i of array) { array[i] = i % 2; }
for...of const
for (const i of array) { array[i] = i % 2; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (10)
Previous results
Fork
Test case name
Result
for var
for let
for var cached length
for let cached length
forEach function
forEach arrow
some function
some arrow
for...of var
for...of const
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its results. **Benchmark Overview** The benchmark is designed to compare the performance of different loop constructs in JavaScript, specifically: 1. Traditional `for` loops with variable declarations (`var`) 2. `let`-declared loops 3. Loops using cached length variables 4. Methods from the Array prototype (`forEach`, `some`) 5. Arrow functions for loops **Options Comparison** The benchmark compares the performance of these different loop constructs, which can be summarized as follows: * **Variable declaration**: The traditional `var` loop and the newer `let`-declared loop. In general, `let` is preferred over `var` due to its scoping rules. * **Loop caching**: Two approaches: using a cached length variable (`l`) inside the loop or not using it. This optimizes the loop by avoiding unnecessary iterations. **Pros and Cons** Here are some pros and cons of each approach: 1. **Traditional `for` loops with variable declarations (`var`)**: * Pros: Widely supported, easy to understand. * Cons: Can lead to performance issues due to scoping rules (e.g., hoisting). 2. **`let`-declared loops**: * Pros: Better scoping control, avoids hoisting issues. * Cons: May require additional setup for loop caching. 3. **Loop caching with `l` variable**: * Pros: Optimizes loop performance by reducing unnecessary iterations. * Cons: Requires careful planning and implementation to avoid errors. **Methods from the Array prototype (`forEach`, `some`)** These methods are designed for iterating over arrays, not loops. However, they can still be used as a benchmark for comparison purposes. **Arrow functions for loops** Arrow functions provide a concise way to define small functions. For loop constructs, arrow functions can simplify code and improve readability but may also lead to performance issues if not optimized correctly. **Benchmark Results** The provided results show the performance of each loop construct across different browsers and versions. The top-performing loops are: 1. `let`-declared loops with cached length variable (`l`) 2. Arrow functions for loops 3. Traditional `for` loops with variable declarations (`var`) 4. Methods from the Array prototype (`forEach`, `some`) Keep in mind that these results may vary depending on specific use cases, code optimizations, and target browsers. In summary, this benchmark provides a comprehensive comparison of different loop constructs in JavaScript, highlighting their pros and cons and performance characteristics.
Related benchmarks:
For vs Foreach vs Do While vs While
for vs foreach vs some vs for..of over real array
For vs Foreach vs Do While vs While v3
for vs every simple
2Sum array: for vs forEach vs for..in vs for..of vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?