Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs some vs for..of vs for in
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of vs for..in
Created:
4 years 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..in
for (var i in array) { 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..in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 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 Edg/141.0.0.0
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
6052462.5 Ops/sec
foreach
11082304.0 Ops/sec
some
11325799.0 Ops/sec
for..of
1410141.6 Ops/sec
for..in
10750010.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** MeasureThat.net is testing the performance of different loop constructs in JavaScript: * `for` loops * `forEach` loops (an array method) * `some` loops (an array method) * `for..of` loops (a newer, more concise syntax for iterating over arrays) These loops are used to iterate over an array of 100 elements. The performance test measures how many executions per second each loop can handle. **Options being compared:** The test compares the performance of four different loop constructs: 1. `for` loops 2. `forEach` loops (an array method) 3. `some` loops (an array method) 4. `for..of` loops Each option has its pros and cons: * **For** loops: * Pros: Simple, widely supported, and easy to understand. * Cons: Can be verbose for large iterations, prone to indexing errors. * **ForEach** loops (array method): * Pros: Concise, easy to read, and automatically handles array lengths. * Cons: May have performance overhead due to the method call, can be less efficient than traditional `for` loops. * **Some** loops (array method): * Pros: Similar to `forEach`, but returns a boolean result indicating whether at least one element matches the condition. * Cons: Also may have performance overhead and is less commonly used. * **For..of** loops: * Pros: Concise, easy to read, and automatically handles array lengths. It's also a more modern syntax that many developers are familiar with. * Cons: Still relatively new, some older browsers or environments might not support it. **Library usage:** None of the provided test cases use any external libraries. They rely solely on built-in JavaScript functionality. **Special JS features or syntax:** There is no mention of special JavaScript features or syntax in this benchmark definition. The tests focus on comparing different loop constructs without introducing any additional complexities. **Alternative approaches:** If you were to design an alternative test, you might consider including other loop constructs, such as: * `while` loops * Recursive functions (for iterating over arrays) * Generator functions or async/await (for iterative computations) Additionally, you could explore testing different array sizes or edge cases (e.g., empty arrays, null or undefined elements) to further validate the performance of each loop construct.
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?