Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Object.assign vs Object.fromEntries vs Object.assign v2
(version: 0)
Comparing performance of:
Reduce vs Map Filter Reduce Assign vs From Entries vs Trivial reduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function createArray(propsCount) { const arr = [] for (let i = 0; i < propsCount; i += 1) { arr.push({ active: i % 5 === 0 }) } return arr } var users = createArray(1000)
Tests:
Reduce
users.reduce((acc, curr) => { if (curr.active) { return acc } return { ...acc, [curr.id]: curr.name } })
Map Filter Reduce Assign
users.filter((user) => !user.active).map((user) => ({ [user.id]: user.name })).reduce(Object.assign, {})
From Entries
Object.fromEntries(users.filter((user) => !user.active).map((user) => [user.id, user.name]))
Trivial reduce
users.filter((user) => !user.active).reduce((acc, {id, name}) => Object.assign(acc, { [id]: name }), {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Reduce
Map Filter Reduce Assign
From Entries
Trivial reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
9167.8 Ops/sec
Map Filter Reduce Assign
8.3 Ops/sec
From Entries
10029.0 Ops/sec
Trivial reduce
5673.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares the performance of four different approaches to merge an object with dynamic keys: 1. `Object.assign()` 2. `Object.fromEntries()` 3. `reduce()` (with a callback function) 4. `trivial reduce` (using a modified version of `reduce()`) **Options Compared** Each approach is being tested on the same input data, which consists of an array of objects with dynamic keys (`id` and `name`). The test aims to measure the performance difference between these four approaches. **Pros and Cons of Each Approach** 1. **Object.assign()**: * Pros: Simple and widely supported. * Cons: May lead to shallow copying, which can result in unexpected behavior if the input object has complex inheritance structures. 2. **Object.fromEntries()**: * Pros: Modern and efficient, creates a new object with the desired structure. * Cons: Introduced in ECMAScript 2015, not supported by older browsers. Requires the `entries()` method to be available. 3. **Reduce()` (with a callback function): * Pros: Flexible and can handle complex merge scenarios. * Cons: May lead to slower performance due to the overhead of iterating over the input array. 4. **Trivial reduce**: * Pros: Optimized version of `reduce()`, avoids unnecessary iterations. * Cons: Requires a custom implementation, which may not be familiar to all developers. **Library and Special JS Features** None of the test cases uses specific libraries or advanced JavaScript features like Web Workers or async/await. The focus is on the core object manipulation functions. **Other Considerations** When evaluating this benchmark, consider the following: * **Browser support**: As `Object.fromEntries()` was introduced in ECMAScript 2015, older browsers might not support it. * **Input data size**: The test case uses a large array of objects (1000 elements). You may want to run the benchmark with smaller input sizes to isolate the performance differences between approaches. * **Contextual factors**: This benchmark focuses on the core object manipulation functions. Consider running additional tests to evaluate other aspects, such as caching or optimization strategies. **Alternatives** If you're looking for alternative approaches or want to explore more advanced techniques: 1. Use a library like Lodash, which provides optimized and flexible merge functions. 2. Implement a custom merge function using `reduce()` with a clever implementation. 3. Consider using `Array.prototype.reduce()` with a binary search approach to optimize performance. Keep in mind that the choice of approach depends on your specific use case, requirements, and target browser support.
Related benchmarks:
Reduce vs Object.assign vs Object.fromEntries
Reduce vs Object.assign vs Object.fromEntries 2
Object.assign vs Object.fromEntries 2
Reduce vs simple assignment vs Object.fromEntries
Comments
Confirm delete:
Do you really want to delete benchmark?