Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of v2
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100).fill(1); let counter = 0; var doStuff = (value) => counter += value;
Tests:
for
for (let i = 0; i < array.length; i++) { doStuff(array[i]); }
foreach
array.forEach((i) => { doStuff(array[i]); });
for in
for (const i in array) { doStuff(array[i]); }
for..of
for (const i of array) { doStuff(array[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
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/125.0.0.0 Safari/537.36 Edg/125.0.0.0
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
69157.1 Ops/sec
foreach
95998.4 Ops/sec
for in
109243.1 Ops/sec
for..of
140495.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its different approaches. **What is tested?** The provided benchmark compares the performance of four different loop constructs in JavaScript: 1. Traditional `for` loop 2. `forEach` method 3. `for...in` loop 4. `for...of` loop (introduced in ECMAScript 2015) Each test case measures the execution time for a specific loop construct, which is executed repeatedly to gather data. **Loop constructs and their pros and cons:** 1. **Traditional `for` loop**: * Pros: simple, efficient, and widely supported. * Cons: can be verbose, especially when dealing with large loops or complex logic. 2. **`forEach` method**: * Pros: concise, easy to read, and well-suited for iterating over arrays or objects. * Cons: may incur a slight performance penalty due to the overhead of the `forEach` method. 3. **`for...in` loop**: * Pros: allows iteration over object properties, including inherited ones. * Cons: can be slow due to the overhead of property access and iteration. 4. **`for...of` loop** (introduced in ECMAScript 2015): * Pros: efficient, concise, and well-suited for iterating over arrays or iterables. * Cons: may not work as expected with older JavaScript engines or versions. **Library usage** None of the test cases use any external libraries. The `forEach` method is a built-in JavaScript function, while `for...in` loop and `for...of` loop are also part of the standard library. **Special JS features or syntax** The benchmark uses modern JavaScript features, including: * Arrow functions (`=>`) * Template literals (`\r\n`) * ECMAScript 2015 syntax (`for...of`) These features may not be supported in older JavaScript engines or versions. **Other alternatives** There are other loop constructs available in JavaScript, such as: * `while` loop * `do-while` loop * `switch` statement (which can be used for iteration over cases) * Generators and iterators (for handling asynchronous or infinite sequences) However, these alternatives are not typically compared to traditional `for` loops, `forEach` method, or `for...in`/`for...of` loops in a benchmarking context. In summary, the benchmark provides a fair comparison of four popular loop constructs in JavaScript, allowing users to evaluate their performance and choose the best option for specific use cases.
Related benchmarks:
Loop Test (forEach vs for)
Array fill foreach, vs for i loop
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?