Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance 4
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
6 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
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** MeasureThat.net is testing the performance difference between two ways of merging objects in JavaScript: using the spread operator (`...`) and `Object.assign()`. The test creates two sample objects, `firstObject` and `secondObject`, with some data. Then it tries to merge these objects into a new object, `finalObject`, using both methods. **Options being compared** There are two options being compared: 1. **Using the spread operator (`...`)**: This method uses the rest/spread syntax to create a new object that includes all properties from `firstObject` and `secondObject`. 2. **Using `Object.assign()`**: This method creates a new object by cloning an existing object (`{}`) and then merges properties from `firstObject` and `secondObject` using the spread operator. **Pros and cons of each approach** 1. **Using the spread operator (`...`)**: * Pros: More concise and readable, especially when merging multiple objects. * Cons: May be slower than `Object.assign()` because it involves creating a new object and then spreading properties into it. 2. **Using `Object.assign()`**: * Pros: Faster than the spread operator, as it only creates a shallow clone of the original object and then merges properties. * Cons: Less concise and less readable, especially when merging multiple objects. **Other considerations** Both methods have their trade-offs in terms of performance and readability. The spread operator is more modern and readable, but may be slower. `Object.assign()` is older and more widely supported, but may be less readable. It's worth noting that the benchmark measures the number of executions per second, which suggests that performance is a key consideration here. **Library** Neither of these methods uses any external libraries. They are built-in JavaScript features. **Special JS feature or syntax** The spread operator (`...`) is a relatively recent addition to JavaScript (introduced in ECMAScript 2015) and is used to create new objects by spreading properties from an existing object. **Benchmark results** According to the latest benchmark result, Firefox 70 on a Mac OS X 10.14 desktop executes `Object.assign()` approximately 27% faster than using the spread operator. **Other alternatives** If you're interested in exploring alternative methods for merging objects, some other options include: * Using `Object.create()`: This method creates a new object by cloning an existing object and then merging properties. * Using a library like Lodash's `merge()` function: This function provides a more concise way to merge objects, but may introduce additional overhead. However, it's worth noting that the benchmark is primarily focused on comparing the performance of these two built-in methods.
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?