Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign()
(version: 0)
Comparing performance of:
Spread vs Assign
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
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
Spread
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):
**Benchmark Overview** The provided benchmark measures the performance difference between two approaches in JavaScript: using the spread operator (`...`) to merge objects, and `Object.assign()` to achieve the same result. **Options Compared** Two options are compared: 1. **Spread Operator (`...`)**: This approach uses the spread operator to create a new object with the properties of the original objects. ```javascript const finalObject = { ...firstObject, ...secondObject }; ``` 2. **Object.assign()**: This approach uses the `Object.assign()` method to merge two or more objects into a single object. **Pros and Cons** **Spread Operator (`...`)** Pros: * Concise and readable syntax. * Creates a new object without modifying the original objects. * Efficient for merging small to medium-sized objects. Cons: * Can be slower than `Object.assign()` for large objects due to the overhead of creating a new array and iterating over it. * Not supported in older browsers (IE 11 and below). **Object.assign()** Pros: * Widely supported across modern browsers, including older versions of Internet Explorer. * Efficient for merging large objects by using the optimized `Object.prototype.toString()` method. Cons: * Less readable syntax compared to the spread operator. * Creates a new object without modifying the original objects, but uses additional memory due to the creation of intermediate arrays. **Other Considerations** Both approaches have their trade-offs. The spread operator is more modern and concise, but may incur performance penalties for very large objects. `Object.assign()` is widely supported, but its syntax can be less readable. Additionally, both approaches do not preserve the original object's prototype chain when merging objects with a different prototype. **Library Usage** Neither of the compared options uses any external libraries. The spread operator is a built-in JavaScript feature introduced in ECMAScript 2018 (ES9), while `Object.assign()` has been part of the standard since ECMAScript 5 (ES5). **Special JS Feature or Syntax** The spread operator (`...`) introduces a new syntax for merging objects, which is relatively modern and widely adopted. It allows for more concise code and improved readability. **Alternative Approaches** If the performance difference between these two approaches is not significant, other options can be considered: 1. **Object.create()**: This method creates a new object with a given prototype, allowing for more control over the resulting object's properties. ```javascript const finalObject = Object.create(firstObject.prototype)(function() { this.sampleData = firstObject.sampleData; this.moreData = secondObject.moreData; }); ``` 2. **Object.assign()` with `Array.prototype.slice()`**: This approach uses `Array.prototype.slice()` to create a shallow copy of the source objects, which can be faster for large objects. ```javascript const finalObject = Object.assign({}, Array.prototype.slice.call(Object.values(firstObject)), Array.prototype.slice.call(Object.values(secondObject))); ``` Keep in mind that these alternative approaches may have their own trade-offs and are not necessarily more efficient than the compared options.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
JavaScript spread operator vs Object.assign performance (single addition)
object spread vs Object.assign
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?