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 finalObject = { sampleData: 'Hello world' }
finalObject.moreData = 'foo bar'
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using Object.assign | |
Using mutation |
Test name | Executions per second |
---|---|
Using the spread operator | 1713069.5 Ops/sec |
Using Object.assign | 4425319.0 Ops/sec |
Using mutation | 934930688.0 Ops/sec |
Let's dive into the benchmark.
What is being tested?
MeasureThat.net is testing the performance of three different approaches to merge two objects in JavaScript:
...
): This syntax allows you to merge two objects by spreading their properties into a new object.Options compared
The benchmark is comparing these three approaches in terms of performance, which means we're interested in how quickly each method can merge two objects.
Pros and Cons of each approach:
...
):Library usage
There is no explicit library mentioned in the benchmark definition. However, Object.assign()
uses the built-in Object
constructor, which is part of the JavaScript standard library.
Special JS features or syntax
The spread operator (...
) was introduced in ECMAScript 2018 (ES2018) and has since become a widely supported feature across modern browsers.
Other alternatives
If you need to merge two objects, other alternatives include:
reduce()
with an initial valueObject.create()
_.merge()
)immer
or similar librariesKeep in mind that the choice of method depends on your specific use case, performance requirements, and personal preference.