Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs Object.fromEntries 2
(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({ id: i, 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 } })
Map Filter Reduce Assign
users.filter((user) => !user.active).map((user) => ({ [user.id]: user })).reduce(Object.assign, {})
From Entries
const a = users.filter((user) => !user.active).map((user) => [user.id, user]) Object.fromEntries(a)
Object Assign
Object.assign( {}, ...users.filter((user) => !user.active).map((user) => ({ [user.id]: user })) )
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
10377.2 Ops/sec
Map Filter Reduce Assign
25.8 Ops/sec
From Entries
34604.1 Ops/sec
Object Assign
1307.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON data represents a benchmark definition, which includes the test cases and their corresponding script preparation code, HTML preparation code, and latest benchmark results. **Tested Options** The benchmark tests four different options for merging objects: 1. **Object.assign**: A built-in JavaScript method that merges two or more objects into one. 2. **Object.fromEntries**: A new JavaScript method introduced in ECMAScript 2019 (ES11), which converts an iterable of key-value pairs into an object. 3. **Map Filter Reduce Assign**: This option involves three separate operations: * **Filter**: Removes elements from the array that do not meet a certain condition. * **Map**: Creates a new array with the results of applying a function to each element in the original array. * **Reduce**: Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. **Pros and Cons** Here are some pros and cons for each option: 1. **Object.assign**: * Pros: Well-established, widely supported, and easy to use. * Cons: Can be slow for large objects, as it involves creating a new object and copying all properties from the source objects. 2. **Object.fromEntries**: * Pros: Newer method, potentially faster than Object.assign, especially for large objects. * Cons: Not yet widely supported in older browsers (pre-ES11). 3. **Map Filter Reduce Assign**: * Pros: Can be faster and more efficient than the other options, as it avoids creating a new object or using a built-in method. * Cons: Requires understanding of array methods and can be less readable. **Library** In the provided benchmark definition, there is no explicit mention of any external libraries. However, **Object.fromEntries** relies on the ECMAScript 2019 (ES11) specification, which may not be fully supported in older browsers. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes mentioned in the provided benchmark definition. **Alternative Approaches** If you need to compare these options in a different context, here are some alternative approaches: 1. **Using Lodash**: You can use the `_.merge()` method from the Lodash library, which provides an efficient and readable way to merge objects. 2. **Using a custom implementation**: You can implement your own merging function using JavaScript's built-in methods, such as `Object.keys()`, `reduce()`, and `forEach()`. 3. **Using a benchmarking framework**: You can use a dedicated benchmarking framework like Jest or Mocha, which provide tools for measuring the performance of your code. **Additional Considerations** When writing benchmarks, consider the following: 1. **Repeatability**: Ensure that the results are repeatable by running multiple iterations of each test case. 2. **Consistency**: Use consistent naming conventions and formatting throughout the benchmark definition. 3. **Clearness**: Make sure the benchmark definition is easy to understand and follow. By considering these factors, you can create a reliable and informative benchmarking tool that helps developers optimize their JavaScript code.
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
copy array: slice vs Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?