Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance - correct
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
4 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 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
17606576.0 Ops/sec
Using Object.assign
12528956.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net! **What is being tested?** The provided benchmark measures the performance difference between two approaches to merge objects: using the spread operator (`...`) and `Object.assign()`. The test case consists of three objects: 1. `firstObject`: containing a single property `sampleData` with value `'Hello world'`. 2. `secondObject`: containing a single property `moreData` with value `'foo bar'`. The benchmark aims to determine which approach is faster when merging these two objects into a new object, `finalObject`. **Options compared:** There are two options being compared: 1. **Using the spread operator (`...`)**: This method uses the rest parameter syntax (`...objectName`) to create a new object and copies properties from `firstObject` and `secondObject` into it. 2. **Using `Object.assign()`**: This method creates a new object using the `Object.assign()` method, passing `firstObject`, `secondObject`, and an empty object as arguments. **Pros and cons of each approach:** * **Using the spread operator (`...`):** + Pros: - More concise and readable code. - Less boilerplate code compared to using `Object.assign()`. + Cons: - May be slower due to the syntax parsing overhead. * **Using `Object.assign()`:** + Pros: - Faster execution since it's a built-in method optimized for performance. + Cons: - More verbose and less readable code. **Library usage:** None of the test cases explicitly use any JavaScript libraries. However, it's worth noting that `Object.assign()` is a built-in method in JavaScript, so no external library is necessary. **Special JS feature or syntax:** The spread operator (`...`) is a relatively recent addition to JavaScript (introduced in ECMAScript 2018). It allows for concise object merging and has become a popular choice among developers. However, as mentioned earlier, it might be slower due to the syntax parsing overhead. **Other alternatives:** If you're looking for alternative approaches to merge objects, consider: 1. **Using `Object.create()`**: This method creates a new object with the specified prototype, and then iterates over the properties of one or more source objects to copy them into the new object. 2. **Using `lodash.assign()` (or other utility libraries)**: If you're working in an environment where performance is critical, consider using optimized library functions like `lodash.assign()`, which may be faster than built-in methods. In conclusion, the provided benchmark helps compare the performance of two popular approaches to merge objects in JavaScript: using the spread operator and `Object.assign()`. While the spread operator offers readability benefits, its syntax parsing overhead might result in slower execution times.
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?