Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for...in vs for...of vs forEach
(version: 3)
Benchmarks the speed of different iteration implementations
Comparing performance of:
for vs for...in vs for...of vs forEach
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function randNum(max, min) { const x = Math.floor(Math.random() * max) if (!isFinite(min) && x < min) { return min } return x } function randString(max, min, base) { let len = randNum(max) if (min && len < min) {len = min} if (len > max) {len = max} let arr = new Uint8Array(len / 2) window.crypto.getRandomValues(arr) return Array.from(arr, (dec)=> ( dec.toString(base || 36).padStart(2, '0') )).join('') } var list = Object.freeze( Array(100).map(()=> Object.freeze({ id: randString(64,64), firstName: randString(40,5), lastName: randString(40,5), email: randString(40,5), address: Object.freeze({ country: randString(40,5), state: randString(40,5), }), })) )
Tests:
for
for (let i = 0; i < list.length; i++) {const el = list[i]; console.count('for')}
for...in
for (const elIdx in list) {const el = list[elIdx]; console.count('for...in')}
for...of
for (const el of list) {console.count('for...of')}
forEach
list.forEach((el)=> console.count('forEach'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
for...in
for...of
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
2525.8 Ops/sec
for...in
5711178.5 Ops/sec
for...of
2500.3 Ops/sec
forEach
1042885.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **What's being tested:** The provided benchmark tests four different iteration methods in JavaScript: 1. **Traditional `for` loop**: Iterates over an array using a manual index variable (`i`) that increments on each iteration. 2. **`for...in` loop**: Iterates over an array using the `in` operator, which returns the property names of an object (in this case, the elements of the array). 3. **`for...of` loop**: Iterates over an array using a new syntax that allows iterating directly over the elements of an array without needing to access their indices. 4. **`forEach` method**: A built-in method of arrays that calls a callback function for each element in the array. **Options compared:** Each test case compares two or more iteration methods, allowing users to see which one is faster on their system. The comparison options are: * `for` vs. `for...in` vs. `for...of` vs. `forEach` * Each pair of methods is tested individually **Pros and Cons:** 1. **Traditional `for` loop**: Fast, but requires manual index management. 2. **`for...in` loop**: Slow due to property iteration, which can lead to slower performance for large arrays. 3. **`for...of` loop**: Faster than `for...in`, as it uses an optimized iterator that bypasses the property lookup. 4. **`forEach` method**: Fast and convenient, but may incur additional overhead due to the need to call a callback function. **Special considerations:** * The benchmark uses randomly generated strings and objects to create a large array (`list`) for testing. * The `for...in` loop is slower because it needs to iterate over the property names of each object in the array, which can lead to additional overhead. **Other alternatives:** If you're interested in exploring other iteration methods or customizing your benchmark, consider using: 1. **`map()` method**: Another built-in method that applies a function to each element in an array. 2. **`reduce()` method**: A built-in method that accumulates values in an array by applying a reduction function. However, these alternatives are not tested in this benchmark and may produce different results. Keep in mind that the best iteration method for your use case depends on specific requirements, such as performance, readability, or convenience.
Related benchmarks:
repeated Math.random() vs crypto.getRandomValues()
Math.random vs Crypto.getRandomValues for 10k values
Object.keys + for vs Object.values + for vs for...in
64k bytes of crypto.getRandomValues vs Math.Random (part 2)
Comments
Confirm delete:
Do you really want to delete benchmark?