Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
8 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 tests the performance of two methods for combining JavaScript objects: the spread operator (`...`) and `Object.assign()`. **Let's break down each method:** * **Using the Spread Operator (`...`)**: This approach involves creating a new object literal and using the spread syntax to include properties from existing objects within it. ```javascript const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; const finalObject = { ...firstObject, ...secondObject }; ``` * **Using `Object.assign()`**: This method takes a target object and one or more source objects, copying properties from the sources into the target. ```javascript const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; const finalObject = Object.assign({}, firstObject, secondObject); ``` **Pros and Cons:** * **Spread Operator:** * **Pros:** Concise and readable syntax. Often considered more intuitive for object merging. * **Cons:** Can be slightly slower than `Object.assign()`, especially with a large number of objects. * **`Object.assign()`:** * **Pros:** Generally faster than the spread operator, particularly with many source objects. More flexible as it allows you to specify the target object and modify existing properties. * **Cons:** Syntax can be less intuitive compared to the spread operator. **Other Considerations:** * **Object Size:** The performance difference between these methods might be negligible for small objects. For very large objects, `Object.assign()` might offer a more noticeable speed advantage. * **Readability:** Choose the method that makes your code easier to understand and maintain. If readability is paramount, the spread operator's simplicity might be preferred. **Alternatives:** While `Object.assign` and the spread operator are common methods for object merging, you could also consider using libraries like Lodash or Ramda, which provide specialized functions for object manipulation with potentially optimized performance. Let me know if you have any more questions.
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?