Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
javascript loops
(version: 1)
Comparing performance of:
foreach vs for-in vs for-of vs for vs optimized-for
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
foreach
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; items.forEach(i => index++);
for-in
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i in items) { index++; }
for-of
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for (let i of items) { index++; }
for
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for(let i = 0; i < items.length; ++i) { index++; }
optimized-for
const items = Array.from({length: 1000}, () => Math.floor(Math.random() * 40)); let index = 0; for(let i = items.length; i > 0; --i) { index++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
foreach
for-in
for-of
for
optimized-for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 days ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 147 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
6569.5 Ops/sec
for-in
5160.0 Ops/sec
for-of
6979.3 Ops/sec
for
6770.2 Ops/sec
optimized-for
6623.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Definition** The benchmark is designed to test the performance of different JavaScript loop constructs: `forEach`, `for-in`, `for-of`, and traditional `for` loops, as well as an optimized version of the `for` loop. The benchmark measures the time taken by each loop construct to iterate over a large array of random numbers. **Options Compared** The benchmark compares four main options: 1. **`forEach`**: This method calls a provided callback function for each element in an array, without requiring explicit index management. 2. **`for-in`**: This loop construct iterates over the properties (including indices) of an object using the `in` operator. 3. **`for-of`**: This is a newer loop construct introduced in ECMAScript 2015, which allows iterating over arrays or iterators without requiring explicit index management. 4. **Traditional `for` loop**: This classic loop construct uses a counter variable to iterate over an array. **Pros and Cons** Here's a brief summary of the pros and cons of each option: * **`forEach`**: Pros: concise, easy to read, and maintain. Cons: can be slower due to the overhead of function calls and potentially more memory allocations. * **`for-in`**: Pros: works on both arrays and objects. Cons: can be slower due to the overhead of accessing properties using `in`, and may not work well with arrays that have a large number of numeric indices (which are assumed to be property names). * **`for-of`**: Pros: concise, easy to read, and maintain. Cons: requires support for modern JavaScript versions; can be slower due to the overhead of creating an iterator. * **Traditional `for` loop**: Pros: widely supported, fast, and flexible. Cons: may require more boilerplate code. **Optimized `for` Loop** The optimized version of the `for` loop uses a technique called "iterative increment" instead of incrementing the counter variable using the ++ operator. This optimization can reduce the overhead of incrementing the counter variable, potentially leading to faster performance. **Libraries and Special Features** In this benchmark, none of the test cases use any external libraries or special JavaScript features beyond what's included in the standard ECMAScript specification. **Other Alternatives** For more comprehensive loop performance comparisons, consider using other benchmarks like: * jsPerf * Benchmark.js * Google's Closure Compiler These tools provide a wider range of test cases and can offer more detailed insights into the performance characteristics of different JavaScript loops.
Related benchmarks:
For Each comparison
test124578
variety of loops in js
lodash forEach vs array.forEach vs for loop
all variety of loops in js
Comments
Confirm delete:
Do you really want to delete benchmark?