Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for with cached length vs foreach vs for..of
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for with cached length vs for with pre-cached length
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100); var alen = array.length;
Tests:
for
for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(i) { array[i]; });
for..of
for (var i of array) { array[i]; }
for with cached length
for (var i = 0, len = array.length; i < len; i++) { array[i]; }
for with pre-cached length
for (var i = 0; i < alen; i++) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for with cached length
for with pre-cached length
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 dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The provided JSON represents a benchmark test that compares the performance of different loop constructs in JavaScript: `for`, `foreach`, and variations thereof (`for with cached length` and `for..of`). The goal is to determine which approach yields the fastest execution time. **Loop Constructs Compared** 1. **Classic `for` loop**: This is the most basic type of loop, where you increment a counter variable (`i`) manually. 2. **`foreach` loop**: This loop iterates over an array using a callback function that receives the current element as an argument. 3. **`for..of` loop**: Introduced in ECMAScript 2015 (ES6), this loop is designed to iterate over arrays and other iterable objects, providing a more concise syntax than traditional `for` loops. 4. **`for with cached length`**: In this variation, the array's length is stored in a separate variable (`alen`) and used to increment the counter (`i`). This approach avoids the overhead of repeatedly accessing `array.length`. 5. **`for with pre-cached length`**: Similar to the previous point, but the loop increments `i` using the cached value of `alen`. **Pros and Cons** * **Classic `for` loop**: Lightweight, easy to understand, and supports various iterations (e.g., `while`, `do-while`). However, manual counter management can lead to errors. * **`foreach` loop**: Provides a concise syntax for iterating over arrays. It's often more readable than traditional `for` loops but may have performance overhead due to callback function creation. * **`for..of` loop**: Offers a modern, expressive syntax that eliminates the need to manually increment a counter. However, it might not be supported in older browsers or Node.js versions. **Library and Special Features** None mentioned in this specific benchmark definition. **Special JS Features or Syntax** The `for..of` loop was introduced as a new iteration mechanism in ES6 (2015), providing an elegant way to iterate over arrays without manually incrementing counters. This syntax is more modern, readable, and efficient than traditional `for` loops for many cases. **Alternatives** Other alternatives to these loop constructs include: * **While loops**: Use the `while` keyword to create a loop that continues as long as a condition is true. * **Do-while loops**: Similar to while loops but with an additional initialization statement before the loop body.
Related benchmarks:
for (cached length) vs foreach vs some
foreach vs for..of
foreach vs for...of
for vs for cached length vs foreach vs some vs for..of
for (cache length) vs foreach vs for..in vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?