Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..of over entries
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100}); var t;
Tests:
for
for (let i = 0; i < array.length; i++) { t = array[i]; }
foreach
array.forEach(function(v, i) { t = v; });
for..of
for (var v of array) { t = v; }
for..of over entries
for (var [i, v] of array.entries()) { t = v; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for..of over entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
136570.0 Ops/sec
foreach
137362.6 Ops/sec
for..of
138051.1 Ops/sec
for..of over entries
132857.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare the performance of four different loop constructs in JavaScript: `for`, `foreach`, `for..of`, and `for..of over entries`. The goal is to determine which construct is the most efficient for iterating over an array. **Loop Constructs Compared** Here's a brief overview of each loop construct compared: 1. **`for`**: A traditional loop construct that uses a counter variable (`i`) to iterate over the array elements. 2. **`foreach`**: A loop construct that iterates over the array using the `forEach` method, which calls a callback function for each element in the array. 3. **`for..of`**: A modern loop construct that uses an iterator (the `v` variable) to iterate over the array elements. This approach is more concise and expressive than traditional `for` loops. 4. **`for..of over entries`**: An extension of the `for..of` loop construct, which uses the `entries()` method of the array to get an iterator over its own indices and values. **Pros and Cons** Here are some pros and cons of each loop construct: 1. **`for`**: * Pros: Widely supported, easy to understand. * Cons: More verbose, less concise than modern alternatives. 2. **`foreach`**: * Pros: Concise, easy to read. * Cons: Can be slower due to the overhead of function calls, not as efficient as `for..of`. 3. **`for..of`**: * Pros: More concise, expressive, and efficient than traditional `for` loops. * Cons: Requires support for modern JavaScript features (ECMAScript 2015+). 4. **`for..of over entries`**: * Pros: Offers a more intuitive way to iterate over both indices and values of an array. * Cons: May be slower due to the overhead of `entries()`, less widely supported than traditional loop constructs. **Library or Special JS Feature Used** None of the provided test cases rely on any external libraries or special JavaScript features beyond those mentioned earlier. However, it's worth noting that some modern browsers may require enabling experimental features (e.g., `for...of over entries`) in their developer tools to see them work correctly. **Other Alternatives** Some alternative loop constructs or approaches could be considered for benchmarking: * **`while` loops**: Traditional loops that use a conditional statement to iterate. * **`reduce()` method**: An array method that can be used to iterate and accumulate values, but may not be as efficient as traditional loops in this context. * **Lambda functions with `map()` or `filter()`**: Using lambda functions with these methods to process arrays, but again, might not be as efficient as traditional loops. Keep in mind that the choice of loop construct ultimately depends on the specific use case and requirements.
Related benchmarks:
foreach vs for..of
Get index with forEach vs for...of over entries
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?