Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for..of big (over a million runs)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1714176);
Tests:
for
for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(i) { array[i]; });
some
array.some(function(i) { array[i]; });
for..of
for (var i of array) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
some
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
4.1 Ops/sec
foreach
224.6 Ops/sec
some
221.2 Ops/sec
for..of
5.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmarking test on the MeasureThat.net website. The benchmark compares the performance of four different loop structures: traditional `for`, `forEach`, `some`, and the newer `for..of` syntax. Let's break down each option: 1. **Traditional `for`**: This is the most basic type of loop, where the index variable `i` is explicitly declared and incremented. The pros of this approach are: * High performance, as it allows for direct indexing into the array. * Easy to implement and understand. The cons are: * Index-related errors can be tricky to avoid. 2. **`forEach`**: This loop structure iterates over an array using a callback function that is applied to each element. The pros of this approach are: * Less prone to index-related errors, as the callback function handles indexing internally. * Can be more readable and maintainable for some use cases. The cons are: * May incur additional overhead due to the callback function's execution. 3. **`some`**: This loop structure returns `true` as soon as it finds an element that passes a test, short-circuiting further iterations. The pros of this approach are: * Can be more efficient for large arrays, as it stops iterating early. The cons are: * Less intuitive and less readable than other options. 4. **`for..of`**: This newer syntax uses the `for...of` loop to iterate over an array, which is similar to `forEach`. However, it provides a more concise and expressive way of writing loops. The pros of this approach are: * More concise and readable than traditional `for`. The cons are: * May incur additional overhead due to the new syntax's parsing. Now, let's analyze the latest benchmark results: The results show that: * **`forEach`** performs best with an average execution rate of 224.59 executions per second. * **`some`** is close behind, with an average execution rate of 221.20 executions per second. * **`for..of`** and **traditional `for`** perform significantly slower, with average execution rates of 5.38 and 4.15 executions per second, respectively. The reasons for these differences are: * **`forEach`** likely incurs additional overhead due to the callback function's execution. * **`some`** stops iterating early, which is beneficial for large arrays. * **`for..of`** and traditional `for` may have slower performance due to their syntax and parsing overhead. Other alternatives that are not included in this benchmark but worth mentioning include: * **Arrow functions**: These can be used with `forEach` or `some` loops, providing a concise way of writing callbacks. * **Generator functions**: These can be used with `for...of` loops, allowing for more expressive and efficient iteration over arrays. Overall, the results suggest that **`forEach`** is the most performant option, followed closely by **`some`**. The newer **`for..of`** syntax and traditional `for` are slower due to their syntax and parsing overhead.
Related benchmarks:
for vs foreach vs some with 10k data
foreach vs for..of
For loop vs <Array>.forEach() vs for...of loop
for (cache length) vs foreach vs for..in vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?