Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..of over entries vs for in
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries vs for..in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100}); var t;
Tests:
for
for (let i = 0; i < array.length; i++) { t = array[i]; }
foreach
array.forEach(function(v, i) { t = v; });
for..of
for (var v of array) { t = v; }
for..of over entries
for (var [i, v] of array.entries()) { t = v; }
for..in
for (var k in array) { t = array[k]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for..of over entries
for..in
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
78832.0 Ops/sec
foreach
79589.6 Ops/sec
for..of
78262.2 Ops/sec
for..of over entries
76072.1 Ops/sec
for..in
68624.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down what is being tested and compared here. **What is being tested?** The benchmark is testing the performance of different ways to iterate over an array in JavaScript: `for`, `forEach`, `for...of`, `for...of` over `entries`, and `for...in`. **What options are compared?** Here are the five test cases: 1. **`for`**: A traditional `for` loop with a counter variable `i`. 2. **`forEach`**: Using the `Array.prototype.forEach()` method to iterate over the array. 3. **`for...of`**: Using a `for...of` loop to iterate directly over the array values. 4. **`for...of` over entries**: Using a `for...of` loop to iterate over the key-value pairs of an array using `entries()`. 5. **`for...in`**: Using a `for...in` loop to iterate over the property names ( indices ) of an object, but here we are using it on an array. **Pros and Cons:** Here's a brief summary: * **`for`**: Pros: simple, familiar syntax; Cons: requires indexing variable. * **`forEach`**: Pros: concise syntax for iterating over arrays; Cons: may not be suitable for large datasets or performance-critical code. * **`for...of`**: Pros: concise and expressive syntax; Cons: may not work with older browsers (e.g., IE). * **`for...of` over entries**: Pros: allows iteration over both key-value pairs and array indices; Cons: requires modern JavaScript features (`entries()` method). * **`for...in`**: Note that using `for...in` on an array is generally discouraged, as it iterates over the property names (indices) rather than the actual values. This can lead to unexpected behavior if the array has non-index properties. **Library usage:** None of these test cases use a specific library. The only external function used is `Array.from()` in the script preparation code. **Special JS features or syntax:** * **`for...of`**: This loop syntax was introduced in ECMAScript 2015 (ES6). * **`entries()` method**: This method returns an array iterator that yields key-value pairs for each entry in an object. It was also introduced in ES6. * **Array.from()**: This method creates a new, dense array from an iterable or an array-like object. **Other alternatives:** For iterating over arrays, you can also use: * `map()` or `filter()` methods (if you need to transform or filter the data) * A simple index-based loop (like `for` loops) when performance is not a concern * Modern JavaScript features like `reduce()`, `every()`, and `some()` (when applicable) Please note that this answer provides a general overview, and the best approach will depend on the specific use case and requirements.
Related benchmarks:
foreach vs for..of
Get index with forEach vs for...of over entries
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?