const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = {
firstObject,
secondObject
};
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = Object.assign({}, firstObject, secondObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign |
Test name | Executions per second |
---|---|
Using the spread operator | 2407404.2 Ops/sec |
Using Object.assign | 5216108.0 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark compares the performance of two approaches to merge two JavaScript objects:
...
)Object.assign()
with an empty object as the target ({}
)Options Compared
...
) is a new way of merging objects introduced in ECMAScript 2018 (ES2018). It allows you to merge two or more objects into one using the syntax object1 [...object2]
.Object.assign()
is an older method for merging objects, which was introduced in ECMAScript 2015 (ES6).Pros and Cons of Each Approach
...
)Pros:
Cons:
Object.assign()
Pros:
Cons:
Library Usage
There is no library being used in this benchmark.
Special JavaScript Features or Syntax
The spread operator (...
) uses a new feature of JavaScript that allows destructuring and spreading objects. This feature was introduced in ECMAScript 2018 (ES2018) and is widely supported across modern browsers and environments.
Other Alternatives
If the spread operator or Object.assign()
were not available, other alternatives could be:
obj1.prop = obj2.prop
)merge()
functionArray.prototype.push()
These alternatives would likely have different performance characteristics and trade-offs compared to the spread operator and Object.assign()
.
Overall, this benchmark is testing the performance of two modern JavaScript approaches for merging objects, which should provide insight into the efficiency and readability trade-offs of each method.