Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of vs for simple vs for..in (+Math.random)
(version: 0)
Comparing performance of:
for..of vs for..in vs for vs foreach
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [12342, 342, 3423, 432, 432, 423, 43, 3, 1, 321, 323, 1, 31, 31, 31, 231, 31, 31];
Tests:
for..of
const narr = []; for(const item of arr){ narr.push(item+Math.random()) }
for..in
const narr = []; for(const item in arr){ narr.push(arr[item]+Math.random()) }
for
const narr = []; for(let i=0; i<arr.length; i++){ narr.push(arr[i]+Math.random()) }
foreach
const narr = []; arr.forEach( e => narr.push(e+Math.random()));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for..of
for..in
for
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 what's being tested on the provided JSON and explain each option. The benchmark is comparing the performance of four different ways to iterate over an array: 1. **`for..of`**: This is a modern, efficient way to iterate over arrays in JavaScript. It was introduced in ECMAScript 2015 (ES6) as part of the Array.prototype.forEach() method. The `for...of` loop uses a special type of variable called an "iterator" to access each element in the array. 2. **`for simple`**: This is a traditional, old-school way to iterate over arrays in JavaScript. It uses a counter variable (`i`) and checks against the length of the array to determine when to stop iterating. 3. **`for...in`**: This method also iterates over properties (in this case, elements) of an object, but it's not suitable for arrays because it returns both own properties and inherited properties. In the context of `arr`, this is likely intended to measure performance of accessing array elements by index. 4. **`forEach`**: This is a modern method to iterate over arrays in JavaScript. It uses a callback function to process each element. Now, let's discuss the pros and cons of each approach: * **Pros**: * `for...of`: Fast and efficient because it uses an iterator to access elements without having to worry about indices. * `forEach`: Also fast and efficient because it's designed specifically for iterating over arrays. It provides a nice abstraction and can handle asynchronous operations easily. * `for simple`: May be slower than the other methods since it requires manual index management, which can introduce overhead due to checks and updates. * **Cons**: * `for simple`: Can lead to off-by-one errors or incorrect indexing if not handled carefully. Also, this method does not provide any guarantee of execution order between iterations. For `for...in` and `forEach`, the performance difference is likely due to the overhead of accessing elements by index (`arr[item]`) compared to direct access using `arr[i]`. Other considerations: * **Memory usage**: All four methods have similar memory usage since they don't require storing any additional state or context during execution. * **Code readability and maintainability**: Each method has its own trade-offs in terms of code simplicity, but `for...of` is generally considered to be one of the most readable options. In summary, if you need to iterate over an array and want a fast, efficient solution with good performance and readability, consider using either `for...of` or `forEach`. If you're dealing with legacy codebases that require manual indexing (which might lead to errors), then `for simple` may be acceptable but use it with caution. If there's no special requirement for iteration, other alternatives include: * **Using a library like underscore.js**: Provides various utility functions for iterating over collections. * **Looping with traditional index management**: Suitable when you know exactly how many elements will be in the collection and can manage indices manually without errors.
Related benchmarks:
Array: spread operator vs push #2
for with saved length vs evaluation on every loop cycle
Array.sort vs Math.min+Math.max (LONG ARRAYS)
Array.prototype.reduce() vs for loop sum
JS fastest unique array Set vs uniq vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?