Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
New Object from Reduce vs. ForEach
(version: 0)
Tests the difference in creating new objects by using reduce vs building up an object in a forEach function
Comparing performance of:
Populate object via ForEach vs Populate object via Reduce
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var objects = Array.from(new Array(1000)).map((r, i) => ({ id: `id-${i}`, val: i })); // Output should be // { "id-0": 0, "id-1": 1, ... }
Tests:
Populate object via ForEach
var newMapFE = {}; objects.forEach(o => { newMapFE[o.id] = o.val })
Populate object via Reduce
objects.reduce((acc, item) => ({...acc, [item.id]: item.val}), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Populate object via ForEach
Populate object via Reduce
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Populate object via ForEach
36952.2 Ops/sec
Populate object via Reduce
19.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a platform that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares two approaches to creating new objects in JavaScript: using the `forEach` method versus the `reduce` method. **Test Cases** There are two test cases: 1. **Populate object via ForEach**: This test case uses the `forEach` method to populate a new object with properties from an array of objects. 2. **Populate object via Reduce**: This test case uses the `reduce` method to populate a new object with properties from an array of objects. **Options Compared** The two options are: 1. Using `forEach` to create a new object 2. Using `reduce` to create a new object **Pros and Cons of Each Approach** **Using `forEach`:** Pros: * Easier to understand and implement, especially for those familiar with iterating over arrays. * Can be more concise in some cases. Cons: * May incur additional overhead due to the iteration process. * Can lead to slower performance compared to other methods. **Using `reduce`:** Pros: * More concise and expressive than using `forEach`. * Optimized for creating objects, as it avoids unnecessary iterations. Cons: * May be less intuitive for those without experience with reduction algorithms. * Can lead to more complex code if not used correctly. **Library Usage** In this benchmark, no libraries are explicitly mentioned. However, the use of `Array.from()` suggests that modern JavaScript features and built-in methods are being utilized. **Special JS Features/Syntax** There is no specific mention of special JavaScript features or syntax in the provided information. The code uses standard JavaScript constructs and features available in most modern browsers. **Alternative Approaches** Other approaches to creating new objects might include: 1. Using a `for` loop: Similar to `forEach`, but with more explicit control over iteration. 2. Utilizing array methods like `map()`: Creates a new array with transformed elements, which can be used to create objects. 3. Leveraging object literals and property chaining: Can be a more concise way to create objects, especially for simple cases. Keep in mind that these alternatives might not provide a direct comparison to the `forEach` and `reduce` approaches, as they often involve different trade-offs between conciseness, readability, and performance.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.fromEntries on array vs reduce on array
Create New Object from Reduce vs. ForEach vs For vs Object.entries
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?