Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce
(version: 0)
Comparing performance of:
forEach vs reduce vs reduce spread
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({length:100},()=>Math.random()); var b = [];
Tests:
forEach
a.forEach(e=>{if (e>0.5) b.push(e)});
reduce
b = a.reduce((a,c)=>c>0.5?a.concat(c):a,[])
reduce spread
b = a.reduce((a,c)=>c>0.5?[...a,c]:a,[])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach
reduce
reduce spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 hours ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
393708.4 Ops/sec
reduce
227054.4 Ops/sec
reduce spread
47384.4 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. **Benchmark Overview** The benchmark compares three approaches for iterating over an array: 1. `forEach` 2. `reduce` (without using the spread operator) 3. `reduce` with the spread operator (`reduce spread`) **Options Compared** Here are the options being compared: * `forEach`: This method calls a provided callback function once for each element in an array, without altering the original array. * `reduce`: This method applies a reduction function to each element in an array and reduces it to a single value. In this case, we're using it as a simple iteration method, comparing its performance with `forEach`. * `reduce spread`: This is similar to `reduce`, but uses the spread operator (`[...a, c]`) to create a new array for each iteration. **Pros and Cons of Each Approach** Here's a brief summary: * `forEach`: + Pros: Simple and widely supported, easy to understand. + Cons: May be slower due to the creation of an intermediate array (in modern JavaScript engines). * `reduce` (without spread): + Pros: Can be more efficient than `forEach`, as it avoids creating an intermediate array. + Cons: Less intuitive for simple iteration tasks, may require more memory allocation. * `reduce spread`: + Pros: Combines the benefits of both approaches, allowing for more flexibility in iteration and avoiding unnecessary array allocations. + Cons: May be less efficient than using `forEach`, due to the additional overhead of creating a new array. **Library Usage** In this benchmark, none of the libraries are explicitly mentioned. However, it's likely that modern JavaScript engines (like V8) are implicitly used as the runtime environment for these tests. **Special JS Features or Syntax** There doesn't seem to be any special features or syntax being tested in this benchmark. **Alternative Approaches** Other alternatives for iterating over arrays include: * Using `for` loops, which can be more efficient than array methods but may not provide the same level of abstraction. * Utilizing libraries like `lodash.each` or `ramda.forEach`, which offer additional functionality and optimizations for iteration tasks. * Leveraging modern JavaScript features like `async/await` or `Generator functions` for asynchronous iteration. Keep in mind that these alternatives might not be directly comparable to the `forEach`, `reduce`, and `reduce spread` methods, as they often require different programming paradigms or context.
Related benchmarks:
for vs foreach vs some
for vs foreach vs some
for vs foreach vs some vs for..of non-empty array square root
javascript loops with reduce 4
javascript loops with reduce 5
Comments
Confirm delete:
Do you really want to delete benchmark?