Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance2
(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 firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign({}, 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two methods for combining JavaScript objects: the spread operator (`...`) and `Object.assign()`. **Here's a breakdown:** * **Spread Operator (`...`)**: This syntax takes the values from an object and expands them individually. In this case, it creates a new object containing all properties from `firstObject` followed by all properties from `secondObject`. ```javascript const finalObject = { ...firstObject, ...secondObject }; ``` * **`Object.assign()`**: This method copies the enumerable properties of one or more source objects to a target object. ```javascript const finalObject = Object.assign({}, firstObject, secondObject); ``` **Pros and Cons:** * **Spread Operator:** * **Pros:** More concise and often considered more readable. * **Cons:** Can be less performant for very large objects due to how it iterates through properties. * **`Object.assign()`**: * **Pros:** Generally more performant for larger objects, as it's optimized for copying properties efficiently. * **Cons:** Less concise syntax compared to the spread operator. **Other Considerations:** * **Performance Trade-offs:** The benchmark results show that `Object.assign()` is faster in this specific case. However, the performance difference may not be significant in most real-world scenarios. * **Code Readability:** The spread operator often leads to more readable and maintainable code for simple object merging tasks. **Alternatives:** While these are the two primary methods, there are other approaches: * **Manual Object Merging:** You can write your own loop-based logic to iterate through objects and copy properties. This gives you maximum control but can be less concise. * **Libraries/Utilities:** JavaScript libraries like Lodash or Ramda offer helper functions for object merging with various options and optimizations.
Related benchmarks:
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance 2 - kevin
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?