Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs property add
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs add
Created:
5 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
let firstObject = { sampleData: 'Hello world' } firstObject = Object.assign(firstObject, { moreData: 'foo bar' });
add
const firstObject = { sampleData: 'Hello world' } 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
add
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 and explain what's being tested. **Benchmark Overview** The test compares three different approaches to add or merge properties to an object: 1. Using the spread operator (`...`) 2. Using `Object.assign()` 3. Adding properties directly to the object (`moreData = 'foo bar'`) **Test Case 1: Using the Spread Operator** In this test, we create two objects: `firstObject` with a single property `sampleData`, and `secondObject` with another property `moreData`. We then use the spread operator (`...`) to merge these two objects into one. The goal is to measure the performance of this approach. **Test Case 2: Using Object.assign()** In this test, we create the same two objects as before and use `Object.assign()` to merge them together. **Test Case 3: Adding Properties Directly** In this test, we modify the original `firstObject` directly by adding a new property `moreData`. This approach is likely to be slower than the first two because it involves re-assigning the object's properties. **Library and Purpose** None of these approaches rely on any external libraries. The spread operator (`...`) is a built-in JavaScript feature introduced in ES6, while `Object.assign()` is also a built-in method. **Special JS Feature or Syntax** The spread operator (`...`) is a new way to merge objects that was introduced in ECMAScript 2015 (ES6). It allows you to expand an object into another object using the syntax `{ ...obj }`. This feature has become widely adopted and is now supported by most modern browsers. **Performance Considerations** The performance of these approaches can vary depending on the specific use case: * **Using the spread operator (`...`)**: This approach is likely to be the fastest because it involves a simple object merge operation, which can be optimized by the browser. * **Using `Object.assign()`**: This approach may be slower than the spread operator because `Object.assign()` involves creating a new object and then assigning properties to it. However, modern browsers have optimized `Object.assign()` for performance. * **Adding Properties Directly**: This approach is likely to be the slowest because it involves re-assigning the entire object's properties. **Other Alternatives** Other approaches to merge objects include: * Using the `concat()` method: `let result = { ...firstObject }; result.concat(secondObject);` * Using the `merge()` function from a library like Lodash * Creating a new object and then assigning all properties using `Object.defineProperty()` However, these alternatives are not tested in this benchmark. Overall, the test provides valuable insights into the performance characteristics of different approaches to merge objects in JavaScript.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
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?