Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance (3)
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
3 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):
**Overview of the Benchmark** The provided benchmark, hosted on MeasureThat.net, compares the performance of two approaches for merging objects in JavaScript: the spread operator (`...`) and `Object.assign()`. The benchmark tests how fast each method can create a new object by copying data from two existing objects. **Options Compared** There are only two options compared: 1. **Using the Spread Operator**: This approach uses the spread operator (`...`) to merge two objects into a new object. For example: ```javascript const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; const finalObject = { ...firstObject, ...secondObject }; ``` 2. **Using `Object.assign()`**: This approach uses the `Object.assign()` method to merge two objects into a new object. For example: ```javascript const firstObject = { sampleData: 'Hello world' }; const secondObject = { moreData: 'foo bar' }; const finalObject = Object.assign({}, firstObject, secondObject); ``` **Pros and Cons of Each Approach** **Using the Spread Operator** Pros: * Concise and readable syntax * Creates a shallow copy of objects (i.e., only copies own enumerable properties) Cons: * Can be slower than `Object.assign()` for large objects or deep merges * Not supported in older browsers **Using `Object.assign()`** Pros: * Faster than the spread operator for large objects or deep merges * Supported in all modern browsers and Node.js versions Cons: * More verbose syntax compared to the spread operator * Creates a shallow copy of objects (i.e., only copies own enumerable properties) **Other Considerations** * The benchmark does not consider other object merge methods, such as `Object.keys()`, `reduce()`, or custom functions. * The test uses JavaScript 2015+ features (e.g., arrow functions, template literals), which may not be supported in older browsers. **Library Used** There is no specific library used in this benchmark. The `Object.assign()` method is a built-in JavaScript function, and the spread operator is a feature introduced in ECMAScript 2015. **Special JS Feature or Syntax** The benchmark uses the following special JavaScript features: * Arrow functions (`=>`) * Template literals (`\r\n`) These features are widely supported in modern browsers and Node.js versions.
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?