Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign wrapper performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
7 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 update = (first, ...args) => { return Object.assign({}, first, ...args) } const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = update(firstObject, secondObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:136.0) Gecko/136.0 Firefox/136.0
Browser/OS:
Firefox Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
7391873.5 Ops/sec
Using Object.assign
5604231.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance of two approaches for merging objects: using the spread operator (`...`) and using `Object.assign()`. The goal is to determine which approach is faster. **Options Compared** Two options are compared: 1. **Using the Spread Operator (`...`)**: This method uses the spread operator to merge two objects into a new object. The syntax is: ```javascript const finalObject = { ...firstObject, ...secondObject }; ``` 2. **Using `Object.assign()`**: This method uses the `Object.assign()` function to merge two objects into a new object. The syntax is: ```javascript const update = (first, ...args) => { return Object.assign({}, first, ...args); }; const finalObject = update(firstObject, secondObject); ``` **Pros and Cons of Each Approach** 1. **Using the Spread Operator (`...`)**: * Pros: More concise and readable syntax. * Cons: May not be supported in older browsers or Node.js versions. 2. **Using `Object.assign()`**: * Pros: Widely supported, including older browsers and Node.js versions. * Cons: Longer syntax and may require more boilerplate code. **Other Considerations** When choosing between these approaches, consider the following: * Code readability: If conciseness is important, using the spread operator might be a better choice. However, if you need to ensure compatibility with older browsers or Node.js versions, `Object.assign()` might be a safer option. * Performance: Both approaches have similar performance characteristics, as they both create a new object and copy the properties from the source objects. **Library and Special JS Feature** There are no libraries used in this benchmark. However, the use of spread operator (`...`) is a modern JavaScript feature introduced in ECMAScript 2018 (ES2018). If you're targeting older browsers or Node.js versions, you may need to use `Object.assign()` instead. **Alternative Approaches** Other alternatives for merging objects include: 1. **Loose Equality Operator (`==`)**: This approach uses loose equality operator to merge properties, but it can lead to unexpected results if the property names are not exactly the same. 2. **JSON Merge Patch**: This approach uses a library like json-merge-patch to merge two objects. It's a more robust and flexible way to handle object merges, especially when dealing with nested objects. In summary, the benchmark measures the performance of two approaches for merging objects: using the spread operator (`...`) and using `Object.assign()`. While both approaches have similar performance characteristics, using the spread operator is generally considered more concise and readable.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?