Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for i < length vs .forEach(t) vs for..of vs for t = array[i] vs for i = 0; i in array vs for i in array vs .reduce (Array)
(version: 4)
Compare loop performance
Comparing performance of:
for i < length vs .forEach(t) vs for..of vs for t = array[i] vs for i=0; i in array vs for i in array, vs .reduce
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 1; i <= 100000; i++) array.push(i);
Tests:
for i < length
for (let i = -1, length = array.length; ++i < length;) { window.t = array[i]; }
.forEach(t)
array.forEach(t => { window.t = t; });
for..of
for (let t of array) { window.t = t; }
for t = array[i]
for (let i = -1, t; t = array[++i];) { window.t = t; }
for i=0; i in array
for (let i = -1; ++i in array;) { window.t = array[i]; }
for i in array,
for (const i in array) { window.t = array[i]; }
.reduce
array.reduce((_, t) => { window.t = t; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
for i < length
.forEach(t)
for..of
for t = array[i]
for i=0; i in array
for i in array,
.reduce
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):
The provided JSON represents a benchmark test case on MeasureThat.net, designed to compare the performance of different loop constructs in JavaScript. **Loop Constructs Compared:** 1. `for (let i = -1; ++i < length;) { ... }`: A traditional `for` loop with a decrementing index. 2. `.forEach(t => { ... });`: The Array.prototype.forEach() method, which iterates over the elements of an array using a callback function. 3. `for (let t of array) { ... }`: The `for...of` loop, introduced in ECMAScript 2015, which iterates over the elements of an array using a single expression. 4. `for (let i = -1, t; t = array[++i];) { ... }`: A "traditional" loop with a post-incrementing index, often considered less efficient than the first two options. 5. `for (let i = -1; ++i in array;) { ... }`: The `for...in` loop, which iterates over the properties of an object (in this case, an array), including both own properties and inherited ones. 6. `const i in array`: This is not a traditional loop construct, but rather a shorthand for iterating over the indices of an array using `for...in`. 7. `.reduce((_, t) => { ... });`: The Array.prototype.reduce() method, which reduces the elements of an array to a single value. **Pros and Cons:** * Traditional `for` loops (1 and 4): Pros - widely supported, easy to understand; Cons - can be verbose and less efficient than other options. * `.forEach(t => { ... });`: Pros - concise, easy to understand; Cons - may have performance issues if the callback function is complex or expensive to evaluate. * `for...of` loop (3): Pros - concise, readable; Cons - may not work as expected in older browsers that don't support it. * Post-incrementing index (`t = array[++i];`) (5 and 7): Pros - can be elegant, Cons - less efficient than traditional indexing methods. * `.reduce((_, t) => { ... });`: Pros - concise, expressive; Cons - may not be suitable for all use cases (e.g., when iterating over multiple arrays). **Other Considerations:** * The `for...in` loop (5 and 6) is less commonly used than other looping constructs, as it can iterate over both own properties and inherited ones. * The `.reduce()` method (7) is a useful alternative to traditional loops, but may not be suitable for all use cases. **Library and Special JS Features:** None of the provided JSON test cases rely on any specific libraries or special JavaScript features beyond the standard ECMAScript 2015 syntax.
Related benchmarks:
foreach vs for..of
Benchmark: flatMap vs reduce vs while vs foreach vs for of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?