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 | 2484038.2 Ops/sec |
Using Object.assign | 3384469.5 Ops/sec |
Let's break down what's being tested in this benchmark.
The benchmark is comparing two approaches to merge two objects in JavaScript:
...
): This syntax was introduced in ECMAScript 2018 (ES2018). It allows you to expand an object literal by spreading its properties onto another object, effectively creating a new object that combines the two.Object.assign()
method: The Object.assign()
method is used to copy the values of all enumerable own properties from one or more source objects into a destination object.Options Compared:
...
)Object.assign()
methodPros and Cons of Each Approach:
...
):Object.create()
or Array.prototype.slice()
.Object.assign()
method:Other Considerations:
Object.assign()
.Library Used: No explicit library is used in this benchmark. However, some browsers might use internal libraries or optimized implementations of these APIs.
Special JS Feature/Syntax: None are explicitly mentioned in the provided code snippets.
Benchmark Preparation Code and JSON:
The provided Script Preparation Code
and Html Preparation Code
are empty, suggesting that the user has not contributed any additional setup or test harness code. The benchmark definitions are stored in a JSON file, which defines the two test cases:
Benchmark Results:
The latest benchmark results show that Chrome 68 executes the Object.assign()
method approximately 34% faster than the spread operator (...
) for this specific use case.
Keep in mind that these results might not be representative of all browsers or scenarios, as the benchmark's scope is limited to a single browser and test setup.