const firstObject = { sampleData: 'Hello world' }
const finalObject = {
firstObject,
{ moreData: 'foo bar' }
};
const firstObject = { sampleData: 'Hello world' }
const finalObject = {
firstObject,
moreData: 'foo bar',
};
const firstObject = { sampleData: 'Hello world' }
const secondObject = { moreData: 'foo bar' }
const finalObject = Object.assign(firstObject, secondObject)
const firstObject = { sampleData: 'Hello world' }
const secondObj = { moreData: 'foo bar' }
const finalObject = {
firstObject,
secondObj,
};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using the spread operator | |
Using single item addition. | |
using object assign | |
Another Bench with Two obj spread |
Test name | Executions per second |
---|---|
Using the spread operator | 2325615.5 Ops/sec |
Using single item addition. | 2322173.8 Ops/sec |
using object assign | 6485152.5 Ops/sec |
Another Bench with Two obj spread | 2292908.2 Ops/sec |
Let's break down the benchmark and its test cases to understand what is being tested.
Benchmark Context
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares different approaches for adding data to an object using the spread operator, single item addition, or the Object.assign()
method.
Test Cases
There are four test cases:
...
) to add two objects together. The first object contains a single property called sampleData
with the value 'Hello world'
. The second object adds another property called moreData
with the value 'foo bar'
....
) to add two objects together. The first object contains a single property called sampleData
with the value 'Hello world'
. The second object contains only one property called moreData
with the value 'foo bar'
.Object.assign()
method to merge two objects together. The first object contains a single property called sampleData
with the value 'Hello world'
. The second object adds another property called moreData
with the value 'foo bar'
....
). However, instead of having a single property in the second object, it contains multiple properties.Library Used
The Object.assign()
method uses the JavaScript built-in Object.assign()
function, which is a part of the ECMAScript standard. This function takes an arbitrary number of source objects and merges them into a target object.
JavaScript Features/ Syntax
...
) is used in all test cases to add data to an object....
.Options Compared
The benchmark compares four different approaches:
...
)Object.assign()
methodPros and Cons of Each Approach
(
...`)...
.Object.assign()
.Other Considerations
The benchmark does not consider factors like performance in different browsers or versions, or the impact of other JavaScript features on the execution time. It only focuses on the specific syntax and approaches being compared.
Alternatives
If you're looking for alternative approaches, here are a few:
{ ...firstObject }
) to merge objects.reduce()
method with an accumulator function to merge objects.