Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs merge mutation
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs MergeObjects function
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign(firstObject, secondObject);
MergeObjects function
function mergeObjects(target, source) { for (const key in source) { if (typeof source[key] === 'object' && source[key] !== null) { if (!target[key] || typeof target[key] !== 'object' || target[key] === null) { target[key] = {}; } mergeObjects(target[key], source[key]); } else { target[key] = source[key]; } } } const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } mergeObjects(firstObject, secondObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
MergeObjects function
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/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
7531426.5 Ops/sec
Using Object.assign
7256352.0 Ops/sec
MergeObjects function
24928908.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark compares the performance of three approaches to merge objects: 1. Using the spread operator (`...`) 2. Using `Object.assign()` 3. A custom `mergeObjects()` function **Options Compared** * Spread Operator (`...`): This syntax creates a new object with all enumerable own properties inherited from the source objects. * `Object.assign()`: This method copies all enumerable own properties from one or more source objects to a target object. * Custom `mergeObjects()` function: This is a custom implementation that recursively merges objects by iterating over their properties. **Pros and Cons** 1. **Spread Operator (`...`)**: * Pros: Concise, readable, and works well with modern browsers. * Cons: Not supported in older browsers (pre-ES6), can lead to unnecessary object creation. 2. **Object.assign()**: * Pros: Wide browser support, efficient for large datasets. * Cons: Can be verbose, may not work correctly if the target or source objects have nested objects with conflicting property names. 3. **Custom `mergeObjects()` function**: * Pros: Flexible, can handle complex object structures and merging logic. * Cons: Requires custom implementation, more overhead due to recursive function calls. **Library Usage** The benchmark uses `Object.assign()`, which is a built-in JavaScript method. It does not rely on any external libraries. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in the benchmark code. The focus is solely on the performance comparison of the three merging approaches. **Alternative Approaches** Other alternatives for merging objects include: * Using `Array.prototype.concat()` with an object constructor (e.g., `{...concat([], { ... })}`) * Utilizing libraries like Lodash (`_.merge()`) or UglifyJS (`merge` function) * Implementing a recursive merge function using recursion instead of iteration * Using modern JavaScript features like `Symbol.for()` to create a unique key for each merged property
Related benchmarks:
JavaScript spread operator vs Object.assign performance to merge into new object
JavaScript spread operator vs Object.assign vs for-in loop performance
JavaScript spread operator vs Object.assign vs for-in loop safe performance
JavaScript spread operator vs Object.assign vs null-checked for-in loop performance
JavaScript spread operator vs Object.assign vs only-null-checked for-in loop performance
Comments
Confirm delete:
Do you really want to delete benchmark?