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 | 5697207.0 Ops/sec |
Using Object.assign | 9763790.0 Ops/sec |
Let's break down what is being tested in this benchmark.
The test case is comparing two approaches to merge objects: using the JavaScript spread operator (...
) and Object.assign()
.
Using the Spread Operator
firstObject
and secondObject
, with some sample data.finalObject
, by spreading the properties of both firstObject
and secondObject
.Using Object.assign()
firstObject
and secondObject
, with some sample data.Object.assign()
method to create a new object, finalObject
, by passing an initial object (firstObject
) and an array of arguments (secondObject
).Pros and Cons
Using the spread operator:
Object.assign()
with a single argument.Using Object.assign():
Library Usage
There is no explicit library used in this benchmark. However, Object.assign()
has been part of the ECMAScript standard since 2015 (ES6) and is widely supported by most modern browsers and engines.
Special JS Features or Syntax
The test uses the spread operator (...
), which is a relatively new feature introduced in ECMAScript 2015 (ES6). It allows for more concise object merging and can improve readability. The syntax was previously only available in certain browsers, but it's now widely supported by modern engines.
Other Alternatives
Before using either the spread operator or Object.assign()
, other alternatives existed:
merge()
function.However, these alternatives are generally less concise, more error-prone, or slower compared to the spread operator and Object.assign()
.