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);
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
let finalObject = {}
finalObject.someData = firstObject.someData
finalObject.moreData = secondObject.moreData
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
Access |
Test name | Executions per second |
---|---|
Using the spread operator | 2102736.5 Ops/sec |
Using Object.assign | 1903698.9 Ops/sec |
Access | 561848000.0 Ops/sec |
Let's break down the provided benchmark definitions and explain what's being tested, compared, and the pros and cons of each approach.
Benchmark Overview
The website MeasureThat.net allows users to create and run JavaScript microbenchmarks. The current benchmark compares three ways to merge objects in JavaScript: using the spread operator (...
), Object.assign()
, and accessing object properties directly.
Individual Test Cases
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = {
...firstObject,
...secondObject
}
The spread operator is being tested in this case.
Pros:
Cons:
sampleData
in firstObject
vs. moreData
in secondObject
). To fix this, you need to use Object.assign()
with a custom merge strategy.const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = Object.assign(firstObject, secondObject)
Object.assign()
is being tested in this case.
Pros:
Cons:
someOtherProperty
in firstObject
).const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
let finalObject = {}
finalObject.someData = firstObject.sampleData
finalObject.moreData = secondObject.moreData
The "Access" test case is being compared against the other two approaches.
Pros:
Cons:
Library Used
None, as this benchmark uses built-in JavaScript features only.
Special JS Feature/ Syntax
The spread operator (...
) is a new feature introduced in ECMAScript 2018. It allows you to expand an object into another object, merging their properties.
For those who may not be familiar with the spread operator, here's a brief explanation:
...
operator followed by an object literal (e.g., { ...firstObject }
), it creates a new object that contains all the properties from firstObject
.[...firstObject]
), it can be used to extract an array of values from an object.Alternatives
If you prefer not to use the spread operator, you can achieve similar results with other methods, such as:
JSON.parse(JSON.stringify(obj1) + JSON.stringify(obj2))
(not recommended due to performance and security concerns)merge()
functionHowever, for most use cases, the spread operator is a concise and efficient way to merge objects in JavaScript.