Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs foreach vs for-of
(version: 0)
Compare loop performance
Comparing performance of:
for-in vs foreach vs for-of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for-in
for (var i = 0; i < array.length; ++i) { array[i]; }
foreach
array.forEach((item) => { item; });
for-of
for (const item of array) { item; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
foreach
for-of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
219625.2 Ops/sec
foreach
9153500.0 Ops/sec
for-of
22089778.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmarking test case, specifically comparing the performance of three different loop iteration methods: `for-in`, `foreach` (or `forEach`), and `for-of`. The benchmark is designed to measure which method is the most efficient in terms of speed. **Loop Iteration Methods Comparison** 1. **For-In Loop**: This method uses a traditional `for` loop with an index variable, similar to the classic C-style loops. It checks each element in the array using the `in` keyword and increments the index manually. ```javascript for (var i = 0; i < array.length; ++i) { array[i]; } ``` **Pros:** * Easy to understand and implement * Works on older browsers that don't support newer loop syntax **Cons:** * Slow due to manual index incrementing * Less efficient than other methods 2. **Foreach Loop (or forEach)**: This method uses the `forEach` function, which is a built-in JavaScript function for iterating over arrays. It takes a callback function that executes once for each element in the array. ```javascript array.forEach((item) => { item; }); ``` **Pros:** * Fast and efficient due to optimized native code * Easy to read and understand **Cons:** * May not be compatible with older browsers (before Chrome 10) * Less control over iteration compared to other methods 3. **For-Of Loop**: This method uses a new loop syntax introduced in ECMAScript 2015 (ES6). It allows iterating over arrays using a `for` loop with a `const` variable that takes the values from the array. ```javascript for (const item of array) { item; } ``` **Pros:** * Fast and efficient due to optimized native code * Easy to read and understand * Compatible with modern browsers **Library Used** None. **Special JS Features or Syntax** No special features or syntax are used in this benchmark, apart from the newer `for-of` loop introduced in ES6. The benchmark is designed to be language-agnostic, making it compatible with a wide range of JavaScript engines and versions. **Other Alternatives** Some alternative loop iteration methods that could have been included in this benchmark are: * **Array.prototype.forEach.call()**: This method uses the `forEach` function and calls it on an array-like object. ```javascript array.forEach.call(array, (item) => { item; }); ``` * **Map iterations**: Some JavaScript engines support iterating over maps using a `for...in` loop, similar to arrays. * **Array.prototype.entries()**: This method returns an iterator for the elements of an array. However, these alternatives are not as commonly used or well-supported as the three methods mentioned in this benchmark.
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?