Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign performance vs manual copy
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Manual copy
Created:
one year 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' } Object.assign(firstObject, secondObject);
Manual copy
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } firstObject.moreData = secondObject.moreData;
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
Manual copy
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
6245951.0 Ops/sec
Using Object.assign
7667370.5 Ops/sec
Manual copy
35895824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark created on MeasureThat.net. The benchmark compares the performance of three approaches: using the spread operator, `Object.assign`, and manual copying. **Test Cases** 1. **Using the Spread Operator** ```javascript const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject } ``` This test case demonstrates the use of the spread operator (`...`) to merge two objects into a new one. 2. **Using Object.assign** ```javascript const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } Object.assign(firstObject, secondObject) ``` This test case uses the `Object.assign()` method to merge two objects into a new one. 3. **Manual Copy** ```javascript const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } firstObject.moreData = secondObject.moreData ``` This test case manually assigns the properties of `secondObject` to `firstObject`. **Options Compared** The benchmark compares the performance of these three approaches: * **Using the spread operator**: Merging two objects using the spread operator (`...`) * **Using Object.assign**: Using the `Object.assign()` method to merge two objects * **Manual copy**: Manually assigning properties from one object to another **Pros and Cons** 1. **Using the Spread Operator** * Pros: + concise syntax + efficient ( creates a new object with shallow copies of properties) * Cons: + may not be suitable for deep objects or complex mergers 2. **Using Object.assign** * Pros: + widely supported and well-documented + flexible (can handle deep objects and complex mergers) * Cons: + may be slower due to the overhead of the `Object.assign()` method 3. **Manual Copy** * Pros: + simple and familiar syntax * Cons: + can lead to errors if not done correctly + inefficient ( creates a new object with shallow copies of properties) **Libraries Used** None. **Special JS Features or Syntax** The spread operator (`...`) is used in the first test case, but it's a standard JavaScript feature introduced in ECMAScript 2018. The `Object.assign()` method is also a standard JavaScript method. **Alternatives** Other alternatives for merging objects include: * Lodash's `merge` function * Underscore.js's `_.merge` * Immutable.js's `merge` These libraries provide more complex and flexible ways to merge objects, but may have additional overhead or dependencies.
Related benchmarks:
JS object copy spread vs assign
JavaScript spread operator vs Object.assign performance for cloning
object.assign vs spread operator for shallow copying large objects 2
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?