Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs. Native Assignment
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Using Native assignment.
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);
Using Native assignment.
const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; firstObject.moreData = 'foo bar';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Using Native assignment.
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. **Benchmark Overview** The benchmark compares the performance of three ways to merge objects in JavaScript: using the spread operator (`...`), `Object.assign()`, and native assignment (`firstObject.moreData = 'foo bar';`). The goal is to determine which approach is the fastest. **Options Compared** 1. **Using the spread operator (`...`)**: * Pros: Modern and concise syntax, easy to read and write. * Cons: May incur additional overhead due to the creation of a new object or array. 2. **Using `Object.assign()`**: * Pros: Standardized and widely supported, can handle more complex data merging scenarios. * Cons: Can be slower than native assignment for simple cases, has a higher memory footprint. 3. **Native Assignment (`firstObject.moreData = 'foo bar';`)**: * Pros: Fastest execution time among the three options, has the lowest memory overhead. * Cons: Non-standard syntax, may not be readable or maintainable for complex cases. **Library Usage** There is no explicit library usage in the benchmark definition. However, it's worth noting that `Object.assign()` relies on the `Object.prototype` property, which might not be the case in all browsers or environments. **Special JavaScript Features/Syntax** None mentioned in the provided data. **Other Considerations** * The benchmark uses a fixed number of objects to merge (`firstObject` and `secondObject`), which may not accurately represent real-world scenarios where objects have different structures and properties. * The benchmark only tests simple assignment of a string value, ignoring more complex merging cases like array or object property updates. **Alternatives** Other alternatives for merging objects in JavaScript include: * Lodash's `merge()` function * Ramda's `merge` function * JSON merge libraries like json-merge or json-migrate These alternatives may offer different performance profiles, syntaxes, and feature sets compared to the options tested in this benchmark.
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?