Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..in vs for..of 2142 34124 214 234 2342323
(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(1000000);
Tests:
for
for (var i = 0; i < array.length; i++) { var bar = array[i]; }
foreach
array.forEach(function(i) { var bar = array[i]; });
for in
for (var i in array) { var bar = array[i]; }
for..of
for (var i of array) { var bar = 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/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
11.5 Ops/sec
foreach
761.6 Ops/sec
for in
1222.4 Ops/sec
for..of
145.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark aims to compare the performance of different loop constructs in JavaScript: * `for` * `foreach` (also known as `forEach` or `Array.prototype.forEach`) * `for...in` (also known as legacy `for` loop) * `for...of` (introduced in ECMAScript 2017) The test is designed to measure the performance of each loop construct on a large array with 1 million elements. **Options Compared** Here's a brief overview of each loop construct: * **`for`**: This traditional loop construct iterates over an array using an explicit index variable. The index variable is incremented at each iteration. + Pros: widely supported, easy to understand, and well-documented. + Cons: can lead to off-by-one errors, and the index variable can become out of sync with the array length if the loop exits early. * **`foreach`**: This loop construct uses an iterator function (e.g., `function(i) { ... }`) that receives each element of the array as an argument. + Pros: concise, easy to read, and well-suited for iterating over arrays. + Cons: may have performance overhead due to the function call overhead, and it's not suitable for non-array iterations. * **`for...in`**: This loop construct iterates over an object using its property names. It can be used with arrays, but it's more commonly associated with iterating over objects. + Pros: convenient when working with objects, easy to understand. + Cons: may not work as expected for array iterations, and it can iterate over inherited properties if the array is nested. * **`for...of`**: This loop construct was introduced in ECMAScript 2017. It uses a simple iteration syntax that receives each element of the array as an argument. + Pros: concise, easy to read, and well-suited for iterating over arrays. + Cons: relatively new feature, may not be supported by older browsers or versions of JavaScript. **Other Considerations** When choosing a loop construct, consider factors like performance, readability, and maintainability. For large array iterations, the `for` loop is often the most efficient choice due to its minimal overhead. However, the `foreach` loop can provide a nice balance between conciseness and readability. In this benchmark, the latest results show that: * `for...in` has the lowest performance due to its overhead in iterating over array elements. * The `for` loop performs reasonably well but is outperformed by the newer `foreach` and `for...of` loops. * The `foreach` loop has a higher execution rate than the `for` loop but may have slightly lower performance due to function call overhead. **Libraries and Special Features** In this benchmark, no external libraries are used. However, some test cases use built-in JavaScript features: * The `Array.prototype.forEach` method is used in the `foreach` loop. * The `for...of` loop uses a new syntax that's available since ECMAScript 2017. Overall, this benchmark provides a useful comparison of different loop constructs in JavaScript and highlights their strengths and weaknesses.
Related benchmarks:
Array fill foreach, vs for i loop
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
Comments
Confirm delete:
Do you really want to delete benchmark?