Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of (z)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 10000000; i++) { array.push(i); } var object = {};
Tests:
for
for (let i = 0; i < array.length; i++) { object[i] = true; }
foreach
array.forEach((i) => { object[i] = true; });
for..of
for (const i of array) { object[i] = true; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
foreach
for..of
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 definition for measuring the performance of three different loop constructs in JavaScript: `for`, `forEach`, and `for..of`. The script preparation code creates an empty array and object, which are used as inputs for the loops. Here's a brief explanation of each test case: 1. **`for`**: This loop uses a traditional `for` loop with an index variable `i` that increments from 0 to `array.length - 1`. The loop pushes values into the `object` using the index. 2. **`forEach`**: This loop uses the `forEach` method, which calls a provided callback function for each element in the array. In this case, the callback function simply sets the value of `object[i]` to `true`, where `i` is the current array index. 3. **`for..of`**: This loop uses a newer `for...of` loop construct, which iterates over the elements of an array using an iterator object. In this case, the loop sets the value of `object[i]` to `true`, where `i` is the current array element. **Options Compared** The benchmark compares the performance of these three loop constructs: * **Pros and Cons:** + `for`: This is a traditional loop construct that provides fine-grained control over indexing. However, it can be verbose and may lead to off-by-one errors. + `forEach`: This loop is more concise and easier to read than the `for` loop. However, it may not provide direct access to the index variable. + `for...of`: This loop provides a convenient way to iterate over arrays without manual indexing. However, it may have performance overhead compared to traditional loops. * **Other Considerations:** + Loop iterations can be affected by factors like array size, JavaScript engine optimizations, and platform-specific nuances. **Library and Special JS Features** The benchmark does not explicitly use any libraries or special JavaScript features beyond the standard `Array` and `Object` classes. However, it's worth noting that the `forEach` loop uses a callback function, which is a common pattern in JavaScript. **Benchmark Results** The latest benchmark results show the performance of each loop construct: * **`for`**: 85.72223663330078 executions per second * **`for..of`**: 16.487634658813477 executions per second * **`forEach`**: 12.495660781860352 executions per second These results indicate that the `for` loop is the fastest, followed by the `for...of` loop, and then the `forEach` loop. **Alternatives** Other alternatives for loop constructs in JavaScript include: * **`while` loops**: These provide more control over indexing but can be less readable than other options. * **`map()` and `reduce()` methods**: These are functional programming constructs that can simplify certain types of looping, but may have performance overhead compared to traditional loops. * **Iterators and generators**: These provide a more efficient way to iterate over arrays and objects, but require a deeper understanding of JavaScript's iterative model. I hope this explanation helps you understand the benchmark definition and its underlying mechanics!
Related benchmarks:
Array fill foreach, vs for i loop
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?