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 | 13449722.0 Ops/sec |
Using Object.assign | 4528875.0 Ops/sec |
Let's dive into the world of JavaScript benchmarks.
What is being tested?
MeasureThat.net is testing two different approaches to merge two objects in JavaScript:
...
syntax)Object.assign()
with an empty object literal ({}
)The benchmark aims to compare the performance of these two methods.
Options compared:
...
syntax)Object.assign()
Object.assign()
with an empty object literal ({}
)Other considerations:
ExecutionsPerSecond
metric is used to measure performance, which indicates the number of executions per second.Library usage:
None. This benchmark does not use any external libraries.
Special JS feature or syntax:
The spread operator (...
) is a relatively new JavaScript feature introduced in ECMAScript 2018. It allows for more concise and expressive object merging.
Benchmark preparation code:
The provided Script Preparation Code
is empty, which means that the benchmark starts with clean slate and no initialization code is executed before running the test cases.
Individual test cases:
There are two test cases:
firstObject
and secondObject
) into a new object (finalObject
).Object.assign()
with an empty object literal ({}
) to merge two objects (firstObject
and secondObject
) into a new object (finalObject
).Latest benchmark result:
The latest results show that:
Object.assign()
.Keep in mind that this is just one specific benchmark, and results may vary depending on the JavaScript engine, version, and other factors.