Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance reassign same variable
(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
let firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } firstObject = {...firstObject, ...secondObject}
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } 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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 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
26536946.0 Ops/sec
Using Object.assign
10592839.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents a benchmark test case for measuring the performance difference between two approaches: using the spread operator (`...`) and `Object.assign()` to merge two objects. **Options Compared** Two options are being compared: 1. **Using the spread operator**: This approach uses the syntax `{...firstObject, ...secondObject}` to create a new object that combines the properties of `firstObject` and `secondObject`. The spread operator (`...`) is used to expand the elements of an array or object. 2. **Using `Object.assign()`**: This approach uses the `Object.assign()` method to merge two objects into one. **Pros and Cons of Each Approach** * **Using the spread operator:** + Pros: - Can be more concise and expressive than using `Object.assign()`. - May be faster since it doesn't require a function call. + Cons: - Requires JavaScript version 4 or later, as older versions don't support the spread operator. - Can be slower for very large objects due to the creation of multiple temporary objects. * **Using `Object.assign()`**: + Pros: - More widely supported across different browsers and JavaScript versions (since it's a built-in method). - Generally faster since it uses a native implementation. + Cons: - Can be less concise than using the spread operator. **Library Used** In this benchmark, no specific library is used beyond the standard JavaScript methods (`Object.assign()`). **Special JS Feature/Syntax** The use of the spread operator (`...`) is a relatively recent feature introduced in ECMAScript 2018. It allows for more concise and expressive object merging and array expansion. **Other Considerations** When benchmarking these two approaches, it's essential to consider factors like: * The size and complexity of the objects being merged. * The browser version and platform being tested. * The presence of other scripts or dependencies that might affect performance. **Alternative Approaches** If you're interested in exploring alternative methods for merging objects, here are a few options: 1. **`.concat()`**: This method involves concatenating two arrays using the `Array.prototype.concat()` method, which can be slower than `Object.assign()`. 2. **`reduce()`**: You can use the `Array.prototype.reduce()` method to merge two arrays by accumulating the results. 3. **Custom implementation**: You can implement your own custom function for merging objects, which might provide better performance but requires more effort. Keep in mind that these alternatives may not be as widely supported or optimized as using `Object.assign()`.
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?