Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs Object.fromEntries
(version: 0)
Comparing performance of:
Reduce vs Map Filter Reduce Assign vs From Entries vs Object Assign
Created:
2 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)
Object Assign
Object.assign( {}, ...users.filter((user) => !user.active).map((user) => ({ [user.id]: user.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
Object Assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 OPR/107.0.0.0
Browser/OS:
Opera 107 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce
26707.5 Ops/sec
Map Filter Reduce Assign
24.4 Ops/sec
From Entries
34742.3 Ops/sec
Object Assign
62288.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks. **Benchmark Purpose** The provided JSON represents two benchmarking tests: `Object.assign vs Object.fromEntries` and four individual test cases (`Reduce`, `Map Filter Reduce Assign`, `From Entries`, and `Object Assign`). These tests aim to measure the performance differences between various approaches for merging or transforming objects in JavaScript. **Comparison of Options** Here's a brief overview of the options being compared: 1. **Object.assign**: This method copies all enumerable own properties from one object to another. 2. **Object.fromEntries**: This method creates a new object from an array of key-value pairs. 3. **Reduce**: A method that applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. 4. **Map Filter Reduce Assign**: An operation consisting of three parts: filtering, mapping, reducing, and assigning the result to a new object using `Object.assign`. 5. **From Entries**: A method that creates a new object from an array of key-value pairs. **Pros and Cons** Here's a brief summary of the pros and cons for each approach: 1. **Object.assign**: * Pros: Simple, widely supported, and efficient. * Cons: Can be slow when dealing with large objects or deep properties. 2. **Object.fromEntries**: * Pros: Modern, concise, and efficient. * Cons: Less widely supported than `Object.assign`. 3. **Reduce**: * Pros: Flexible, allowing for custom accumulation logic. * Cons: Can be slower due to the overhead of function calls. 4. **Map Filter Reduce Assign**: * Pros: Encapsulates multiple operations in a single operation, reducing overhead. * Cons: More complex than individual operations, which can lead to confusion and performance issues if not optimized correctly. 5. **From Entries**: * Pros: Modern, concise, and efficient. * Cons: Less widely supported than `Object.assign`. **Library Usage** None of the provided benchmarks explicitly use external libraries. **Special JS Features or Syntax** There are no special JavaScript features or syntaxes mentioned in the provided benchmarks. **Benchmark Preparation Code** The preparation code for the benchmark is provided, including a custom function `createArray` to generate an array of objects with specific properties. This code is used to create the input data for each test case. **Individual Test Cases** Each test case measures the performance of a specific approach: 1. **Reduce**: Measures the performance of using `reduce` to merge objects. 2. **Map Filter Reduce Assign**: Measures the performance of combining multiple operations: filtering, mapping, reducing, and assigning the result. 3. **From Entries**: Measures the performance of using `Object.fromEntries` to create a new object from an array of key-value pairs. 4. **Object Assign**: Measures the performance of using `Object.assign` to merge objects. **Latest Benchmark Results** The latest benchmark results are provided, showing the execution per second for each test case and browser. **Other Alternatives** For those seeking alternative approaches or more detailed explanations, here are a few suggestions: 1. Explore other methods for merging or transforming objects, such as using `lodash` or custom utility functions. 2. Dive deeper into the implementation details of `Object.assign` and `Object.fromEntries`. 3. Investigate how modern browsers optimize object property iteration and merging. I hope this explanation helps software engineers understand the benchmarking tests and approaches being compared!
Related benchmarks:
Object.assign vs direct copy
Array constructor vs literal performance, 12345
JavaScript assign like array VS Object.assign Performance
Object.fromEntries vs create temp object
Creation of new Array: Array.from vs. new Array
Comments
Confirm delete:
Do you really want to delete benchmark?