Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for..of vs for..in vs foreach
(version: 0)
Compare loop performance
Comparing performance of:
for vs for..of vs for..in vs forEach
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for
for (var i = 0, n = array.length; i < n; i++) { array[i]; }
for..of
for (var i of array) { array[i]; }
for..in
for (var i in array) { array[i]; }
forEach
array.forEach(i=>i)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
for..of
for..in
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explore what each test case is testing, along with their pros and cons. **Benchmark Definition** The benchmark definition compares the performance of four different loop constructs in JavaScript: 1. Traditional `for` loop 2. `for..of` loop (introduced in ECMAScript 2015) 3. `for..in` loop (also known as "iteration" or "indexing" loop, although it's not exactly what you'd expect from its name) 4. `.forEach()` method **Loop Constructs** 1. **Traditional `for` loop**: This is the most common type of loop in JavaScript. It uses an explicit index variable and increment/decrement statements to iterate over a sequence. ```javascript for (var i = 0, n = array.length; i < n; i++) { array[i]; } ``` Pros: * Well-established syntax * Easy to read and write * Suitable for simple, iterative tasks Cons: * Can be verbose and repetitive in complex scenarios * May have performance issues if not optimized correctly 2. **`for..of` loop**: Introduced in ECMAScript 2015, this loop is designed for iterating over iterables (like arrays or objects). ```javascript for (var i of array) { array[i]; } ``` Pros: * More concise and expressive than traditional `for` loops * Simplifies iteration over sequences Cons: * May not be suitable for complex scenarios that require manual index management * May have performance issues if not optimized correctly 3. **`for..in` loop**: This loop is used to iterate over the property names of an object, rather than iterating over a sequence like an array. ```javascript for (var i in array) { array[i]; } ``` Pros: * Can be useful for iterating over objects, especially when working with legacy code Cons: * May not be suitable for iterating over sequences or arrays * Can lead to unexpected results if the object's prototype chain is modified during iteration 4. **`.forEach()` method**: This is a built-in method that allows you to iterate over an array and execute a callback function for each element. ```javascript array.forEach(i => i); ``` Pros: * Concise and expressive syntax * Suitable for simple, iterative tasks * No need to worry about manual index management or performance optimization Cons: * May not be suitable for complex scenarios that require manual index management * Can lead to unexpected results if the array is modified during iteration **Library Used** There is no explicit library mentioned in the benchmark definition. However, the use of `for..in` loop and `.forEach()` method implies that the test uses modern JavaScript features. **Special JS Features/Syntax** There are no special JS features or syntax explicitly used in this benchmark. The tests only use standard JavaScript constructs. **Other Alternatives** If you wanted to compare other loop constructs, here are some alternatives: * `while` loops * Recursive functions * `map()`, `filter()`, and `reduce()` methods (which can be used as alternatives to traditional loops) Keep in mind that the choice of loop construct depends on the specific use case and performance requirements. This benchmark provides a good starting point for comparing the performance of different loop constructs in JavaScript.
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?