Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of vs forEach flo2
(version: 0)
Comparing performance of:
for..of vs for each
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
arr1 = [[1,2,3],[1,2,3],[1,2,3]]
Tests:
for..of
result = [] for(const arr of arr1) { for (const num of arr) { if (num === 2) { result.push(num) continue } } }
for each
result = [] arr1.forEach((arr) => { arr.forEach((num) => { if(num === 2) { result.push(num) } }) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for..of
for each
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):
I'd be happy to help you understand the provided JavaScript benchmark. **What is being tested?** The benchmark compares two approaches: `for..of` and `forEach`. Both methods are used to iterate over an array (`arr1`) and push values into an empty array (`result`) if they meet a certain condition (i.e., if the value is equal to 2). **Options compared:** Two options are being compared: 1. **For-each loop using `for..of`**: This method uses the `for...of` loop syntax, which allows you to iterate over an array without explicitly indexing into it. 2. **Classic for-each loop using `forEach`**: This method uses the `forEach` method, which is a part of the Array prototype. **Pros and Cons:** * **For-each loop using `for..of`:** + Pros: - More concise and readable syntax - Eliminates the need for explicit indexing into arrays - Can be more efficient in some cases (e.g., when iterating over large arrays) + Cons: - Might have worse performance due to additional overhead from the `for...of` loop * **Classic for-each loop using `forEach`:** + Pros: - Widely supported and well-established syntax - Can be more performant in some cases (e.g., when iterating over large arrays) + Cons: - Less readable and concise syntax compared to `for...of` - Requires explicit indexing into the array **Library or special JS feature:** Neither of these options uses a library or a special JavaScript feature. Both methods are part of the standard JavaScript language. **Other alternatives:** If you want to explore alternative approaches, here are a few: * **While loop**: You could use a `while` loop to iterate over the array, but this would require more manual indexing and control. * **Array.prototype.reduce()**: While not directly applicable for this specific problem, `reduce()` can be used to accumulate values into an array. However, it might not provide the same level of performance as the `for...of` or `forEach` methods. * **Other iteration methods (e.g., `map()`, `filter()`)**: These methods are typically used for transforming arrays or filtering out elements, but they can also be used to iterate over an array if desired. In this specific benchmark, the performance difference between `for...of` and `forEach` is relatively small, indicating that both approaches have similar execution times. However, further benchmarks may reveal differences in performance depending on the specific use case and environment.
Related benchmarks:
Iteration through array; of vs forEach
foreach vs for..of
foreach vs for...of
flatten reduce vs for of
flatten reduce vs for .. of vs reduce TCO
Comments
Confirm delete:
Do you really want to delete benchmark?