Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of set vs array and forEach
(version: 1)
Comparing performance of:
array for...of vs set for...of vs array.forEach vs set.forEach
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let arr = Array.from({length:1000},(a,i)=>i), set = new Set(arr), num = 0 function forEach(i) {num += i}
Tests:
array for...of
for(let n of arr) num+= n
set for...of
for(let n of set) num+= n
array.forEach
arr.forEach(forEach)
set.forEach
set.forEach(forEach)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array for...of
set for...of
array.forEach
set.forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array for...of
43344.3 Ops/sec
set for...of
20466.9 Ops/sec
array.forEach
114210.9 Ops/sec
set.forEach
52502.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In this benchmark execution, the performance of different methods for iterating over an array and a set is being compared. Specifically, it assesses the following four test cases: 1. **Array `for...of` Loop**: - **Benchmark Definition**: `for(let n of arr) num+= n` - **Test Name**: "array for...of" - This test iterates over the elements of an array using the `for...of` syntax, which provides a simple way to loop through iterable objects (like arrays). 2. **Set `for...of` Loop**: - **Benchmark Definition**: `for(let n of set) num+= n` - **Test Name**: "set for...of" - Similar to the array test, this one iterates over the elements of a `Set`. A `Set` is a collection of values where each value must be unique. 3. **Array `forEach` Method**: - **Benchmark Definition**: `arr.forEach(forEach)` - **Test Name**: "array.forEach" - This test uses the `forEach` method available on arrays, invoking a callback function (`forEach`) which increments a number variable. 4. **Set `forEach` Method**: - **Benchmark Definition**: `set.forEach(forEach)` - **Test Name**: "set.forEach" - This uses the `forEach` method available on sets, applying the same callback function that was used for the array `forEach`. ### Pros and Cons of Each Approach: 1. **`for...of` Loop**: - **Pros**: - Simple syntax and easy to read. - Works with any iterable (arrays, strings, sets, etc.). - **Cons**: - In general, it might be slower than native methods like `forEach` for larger datasets. 2. **`forEach` Method**: - **Pros**: - Very concise and functional style, which can lead to clearer code. - Directly ties a callback function to the execution, which can be useful for complex operations. - **Cons**: - Generally slower than a `for` loop for larger datasets due to the overhead of function calls. - Cannot be easily broken (i.e., you cannot use `break` or `continue`). ### Other Considerations: - **Data Structure**: The choice between using an array and a set can depend on the specific use case. Arrays allow duplicate values and maintain order, while sets enforce unique values. - **Performance**: The performance results show that `set.forEach` performed the best (executions per second: 22,912.25), while the classic array `for...of` loop performed the least favorably (3,704.17 executions per second). This suggests that, at least in this benchmark, the built-in `forEach` methods are more efficient than the `for...of` loops for both arrays and sets. ### Alternatives: - **Traditional `for` Loop**: While not tested in this benchmark, a traditional `for` loop could be an alternative for iteration over arrays, providing potentially better performance for certain scenarios as it avoids function overhead. - **`for...in` Loop**: This is another JavaScript construct that could be used for iteration, but it is specifically for object properties and is not suitable for arrays or sets, as it may not maintain order. - **Map and Reduce**: For more complex manipulations, methods like `map` and `reduce` could also be utilized on arrays, though they tend to have their own performance characteristics due to their functional approach. Overall, depending on the specific needs in terms of performance, readability, and the nature of data being handled, developers can choose between these different iteration techniques.
Related benchmarks:
for vs foreach for (cached length) vs for..of -- With assignment
for..of vs for vs forEach
for (i < n) vs Array(n).forEach
for vs for..of vs forEach, arrow function
For vs for of vs forEach
for vs foreach vs for..of vs for..of over entries vs for in
for vs foreach vs for-const vs for..of
for vs .foreach vs .some vs for..of
for vs foreach vs for..in vs for..of (Fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?