Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs forEach test
(version: 0)
Reduce vs forEach
Comparing performance of:
reduce vs forEach
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fields = Array.from({ length: 100000 }, (v, i) => ({ fieldName: i, value: i % 2 === 0 }));
Tests:
reduce
var customFields = fields.reduce((acc, { fieldName, value }) => { acc[fieldName] = value; return acc; }, {});
forEach
var customFields = {}; fields.forEach(({ fieldName, value }) => (customFields[fieldName] = value));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
850.7 Ops/sec
forEach
1369.0 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 is designed to compare the performance of two approaches for iterating over an array and creating a new object with the same keys: `forEach` and `reduce`. **What's Being Tested?** The benchmark measures the execution time of each approach on a large array (`fields`) containing 100,000 elements. The array has fields with binary values (true or false) based on the index. **Options Compared** Two options are being compared: 1. **`forEach`**: This method iterates over the array using a callback function, assigning a value to an object key for each iteration. 2. **`reduce`**: This method applies a reduction function to the array, accumulating values in an accumulator object. **Pros and Cons of Each Approach** ### `forEach` Pros: * Easier to understand and implement, especially for developers without prior knowledge of `reduce`. * More straightforward logic, as each iteration is handled individually. * Can be more efficient in some cases due to reduced overhead. Cons: * May incur additional overhead due to the callback function and the need to update an object key on each iteration. * May not be optimized for performance, especially when dealing with large arrays. ### `reduce` Pros: * More efficient in terms of performance, as it uses a single pass through the array and avoids unnecessary updates. * Can be more suitable for large datasets, as it reduces the overhead of individual iterations. Cons: * Requires understanding of the `reduce` method and its accumulator pattern. * May have a steeper learning curve due to the complex logic involved in handling the accumulator object. **Library/Functionality** None are explicitly mentioned in the benchmark code. However, both methods rely on built-in JavaScript functionality: `forEach` uses a native method, while `reduce` is also a native method. **Special JS Feature/Syntax** The benchmark does not utilize any special JavaScript features or syntax beyond the standard methods and array iteration. **Other Alternatives** Other approaches could be considered for this type of task: 1. **Using a custom loop**: Writing a manual loop to iterate over the array would likely have similar performance characteristics to `forEach`. 2. **Using a library like Lodash**: The `_.reduce` method from the Lodash library could be used instead, which might offer improved performance and readability. 3. **Parallel processing**: Using parallel processing techniques (e.g., Web Workers) could potentially speed up the execution time of both approaches. However, these alternatives would likely have different overheads and complexities compared to the standard `forEach` and `reduce` methods, making them less suitable for a general benchmarking comparison.
Related benchmarks:
forEach vs reduce
forEach vs reduce
Math.max() vs Array.reduce() for array of objects
Math.max vs Array.reduce
for-of vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?