Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for..of vs for..of with entries
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of vs for...of with entries
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
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]; }
for...of with entries
for (const [i, value] of array.entries()) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
some
for..of
for...of with entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
128213.6 Ops/sec
foreach
7029449.5 Ops/sec
some
6878393.0 Ops/sec
for..of
209116.5 Ops/sec
for...of with entries
206435.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmarking test, where users can compare the performance of different loop constructs in JavaScript. **What is tested?** The benchmark tests four different loop constructs: 1. `for` loop: A traditional loop using a counter variable (`i`) to iterate over an array. 2. `forEach` loop: An iteration method provided by the Array prototype, which iterates over an array and executes a callback function for each element. 3. `some` loop: A method that returns `true` if at least one element in the array satisfies the condition specified as a callback function. 4. `for..of` loop with and without `entries`: Two variants of the `for...of` loop, which iterate over an array using a more modern approach. The first variant uses the spread operator (`...`) to create an iterator from the array's `entries()` method. The second variant uses the `entries()` method directly. **Options compared** The benchmark compares the performance of these four loop constructs under different conditions: * Number of executions per second * Device platform (Desktop) * Browser (Chrome 126) **Pros and Cons of each approach:** 1. **`for` loop**: * Pros: Simple, easy to understand, widely supported. * Cons: Can be slow due to the overhead of incrementing a counter variable. 2. **`forEach` loop**: * Pros: Modern, concise syntax, efficient for small arrays. * Cons: May have performance issues with large arrays, as it relies on the Array prototype's iteration mechanism. 3. **`some` loop**: * Pros: Fast and efficient for checking conditions on individual elements. * Cons: Not suitable for iterating over arrays element-by-element; its purpose is to check a condition on each element. 4. **`for..of` loop with `entries`**: * Pros: Modern, concise syntax, efficient for large arrays using the spread operator and `entries()` method. * Cons: May have compatibility issues with older browsers or versions of Node.js. The benchmark results show that: * The `forEach` loop is the fastest among all options tested. * The `some` loop is also relatively fast but slower than the `for..of` loops. * The `for..of` loop with and without `entries` are comparable in performance, with slight differences between them. **Other considerations:** * Library usage: None of these loop constructs rely on external libraries or frameworks. However, some browsers (like Chrome) may use internal optimizations or caching that affect performance. * Special JavaScript features/syntax: None mentioned in the provided JSON.
Related benchmarks:
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
for (cache length) vs foreach vs for..in vs for..of
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?