Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Merging Objects
(version: 8)
Comparing performance of:
Reduce / Spread vs Assign / Single spread vs fromEntries / Entries vs Assign / Mutate first obj
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function createSlices(numSlices, propsPerSlice = 5) { const arr = []; for (let i = 0; i < numSlices; i++) { const o = {}; for (let j = 0; j < propsPerSlice; j++) { o[`key_${i}_${j}`] = `${i}_${j}`; } arr.push(o); } return arr; } var slices = createSlices(100);
Tests:
Reduce / Spread
slices.reduce((store, slice) => ({ ...store, ...slice }), {});
Assign / Single spread
Object.assign({}, ...slices);
fromEntries / Entries
Object.fromEntries( slices .map((slice) => Object.entries(slice)) .flat() );
Assign / Mutate first obj
slices.unshift({}); Object.assign(...slices);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Reduce / Spread
Assign / Single spread
fromEntries / Entries
Assign / Mutate first obj
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of different methods for merging objects in JavaScript. The script preparation code generates an array of 100 objects, each with 5 properties. **Options Compared** There are four test cases: 1. **Reduce / Spread**: Using `Array.prototype.reduce()` and the spread operator (`...`) to merge objects. 2. **Assign / Single spread**: Using `Object.assign()` with a single object as an argument (i.e., spreading an array of objects onto a target object). 3. **fromEntries / Entries**: Using `Object.fromEntries()` with an array of key-value pairs. 4. **Assign / Mutate first obj**: Using `Array.prototype.unshift()` to add an empty object at the beginning of the array, followed by `Object.assign()` to merge the objects. **Pros and Cons** * **Reduce / Spread**: This approach is concise and easy to read. It uses a single function call to merge all objects into a single result. * Pros: Efficient, readable * Cons: Can be slower than other approaches if the input array is large. * **Assign / Single spread**: Using `Object.assign()` with an array of objects as arguments can be more efficient than using `reduce()` or `fromEntries()`. * Pros: Fast for small to medium-sized arrays, concise * Cons: Can lead to slower performance if the input array is large. * **fromEntries / Entries**: This approach uses a modern syntax to merge objects from an array of key-value pairs. It can be more efficient than other approaches when working with large datasets. * Pros: Fast for large arrays, concise * Cons: Less intuitive than other approaches * **Assign / Mutate first obj**: Adding an empty object at the beginning of the array and then merging it with `Object.assign()` can be less efficient than using one of the above methods. * Pros: None notable * Cons: Can lead to slower performance due to unnecessary object creation **Library** None mentioned in this benchmark. **Special JS Features or Syntax** The benchmark uses modern JavaScript syntax and features, such as: * Arrow functions (`=>`) * Spread operator (`...`) * `Object.fromEntries()` and `Array.prototype.flatMap()` * Template literals (`${}`) These features are widely supported by modern browsers and engines. **Other Alternatives** Some alternative methods for merging objects could be considered, such as: * Using a library like Lodash's `merge` function * Implementing a custom merge function using recursion or iteration * Using a more functional programming approach with `reduce()` or `map()` However, the benchmark is focused on comparing the performance of specific methods and syntaxes that are commonly used in JavaScript development.
Related benchmarks:
Array clone
slice-splice
splice-slice-b
Splice vs. Spread Slice
Array cloning: slice vs spread
Comments
Confirm delete:
Do you really want to delete benchmark?