Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs for of 7
(version: 0)
Comparing performance of:
forEach vs forOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10000000).fill(0);
Tests:
forEach
arr.forEach(item => item);
forOf
for (const item of arr) item;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
forOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
10.7 Ops/sec
forOf
8.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark. **What is being tested?** The provided JSON represents a benchmark comparison between two approaches: 1. `forEach`: A traditional, iterative method that uses a callback function to process each item in an array. 2. `forOf`: A newer, more modern approach that uses a for-of loop to iterate over the array. **What options are compared?** The main options being compared are: * **Array iteration**: The two methods are used to iterate over an array of 10 million elements (generated using `Array(10000000).fill(0)`). **Pros and Cons of each approach:** 1. **forEach**: * Pros: + Wide compatibility across browsers and platforms. + Easy to understand and implement, especially for those familiar with callback functions. * Cons: + Can be less efficient than `forOf` due to the overhead of function calls and argument passing. 2. **forOf**: * Pros: + More modern and efficient approach, as it avoids the overhead of function calls and argument passing. + Better suited for large datasets, as it can take advantage of caching mechanisms (if enabled). * Cons: + Not yet widely supported by older browsers or platforms. **Library usage:** There is no explicit library mentioned in the benchmark definition. However, note that the `Array.prototype.forEach` method is a part of the ECMAScript standard, and its implementation can vary across browsers and platforms. **Special JS feature or syntax:** No special JavaScript features or syntax are used in this benchmark. The focus is solely on comparing two different iteration approaches. **Other alternatives:** If you're interested in exploring alternative array iteration methods, some examples include: 1. `map()`: A method that creates a new array with the results of applying a provided function to each element. 2. `filter()`: A method that creates a new array with only the elements that pass a test implemented by a provided function. 3. `reduce()`: A method that applies a function to each element in an array and reduces it to a single output value. These alternatives can offer different trade-offs, such as performance, memory usage, or code readability, depending on your specific use case.
Related benchmarks:
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map with large array
Array loop vs foreach vs map (cached length)
Array loop vs foreach vs every
Array loop vs for loop vs for loop cached
Comments
Confirm delete:
Do you really want to delete benchmark?