Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Object.assign vs Object.fromEntries
(version: 0)
Comparing performance of:
Reduce vs Map Filter Reduce Assign vs From Entries
Created:
4 years 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
const a = users.filter((user) => !user.active).map((user) => [user.id, user.name]) Object.fromEntries(a)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce
Map Filter Reduce Assign
From Entries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
40878.6 Ops/sec
Map Filter Reduce Assign
38.8 Ops/sec
From Entries
47269.2 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 Definition** The benchmark is designed to compare three different approaches for performing a common JavaScript operation: 1. `Object.assign()` 2. `Object.fromEntries()` (which was introduced in ECMAScript 2017) 3. Using the `reduce()` method with an accumulator object These three approaches are compared in the following test cases: * `Map Filter Reduce Assign`: This test case uses a combination of `filter()`, `map()`, and `reduce()` methods, as well as `Object.assign()`. * `From Entries` * `Reduce` **Options Compared** The benchmark compares the performance of three different approaches: 1. `Object.assign()`: A simple object assignment method that creates a new object by copying properties from an existing object. 2. `Object.fromEntries()`: A method that creates a new object from an array of key-value pairs. 3. Using the `reduce()` method with an accumulator object: This approach uses the `reduce()` method to iterate over an array and accumulate values in an object. **Pros and Cons** Here are some pros and cons for each approach: 1. **`Object.assign()`**: * Pros: Simple, widely supported, and easy to read. * Cons: Can be slow for large datasets, as it creates a new object by copying properties from an existing object. 2. **`Object.fromEntries()`**: * Pros: Efficient, modern method that was introduced in ECMAScript 2017. * Cons: May not be supported in older browsers or environments. 3. **Reduce with accumulator object**: * Pros: Can be efficient for large datasets, as it avoids creating a new object by copying properties from an existing object. * Cons: Requires understanding of the `reduce()` method and its accumulator parameter. **Library and Purpose** None of the libraries are explicitly mentioned in the benchmark definition. However, `Object.fromEntries()` is a built-in JavaScript method that was introduced in ECMAScript 2017. **Special JS Features or Syntax** The `Map Filter Reduce Assign` test case uses some advanced JavaScript features, including: 1. **Array methods**: `filter()`, `map()`, and `reduce()` are used to process the data. 2. **Object assignment**: `Object.assign()` is used in one of the test cases. These features are widely supported in modern browsers and environments, but may not be understood by older developers or those without experience with these methods. **Alternatives** If you're looking for alternative approaches or want to explore other options, here are a few suggestions: 1. Using `lodash` library: Lodash provides an implementation of the `assign()` method that can be faster than the built-in `Object.assign()`. 2. Using `merge()` method from `lodash`: Similar to `Object.assign()`, but with some performance improvements. 3. Using `reduce()` method with a custom accumulator object: This approach can be efficient for large datasets, but requires understanding of the `reduce()` method and its accumulator parameter. It's worth noting that these alternatives may not provide significant performance improvements over the original benchmark, as they are often implemented in native code or have been optimized for performance.
Related benchmarks:
Array constructor vs literal performance, 12345
Object.fromEntries vs create temp object vs Array.reduce
new Array from vs slice vs push vs index vs spread (with array prepared)
test1235161321
Array.from vs array destructure
Comments
Confirm delete:
Do you really want to delete benchmark?