const firstObject = { sampleData: 'Hello world', moreData: 'foo bar' }
const finalObject = {
firstObject,
};
const firstObject = { sampleData: 'Hello world', moreData: 'foo bar' }
const finalObject = Object.assign({}, firstObject);
--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 | 48661816.0 Ops/sec |
Using Object.assign | 3982634.8 Ops/sec |
Let's dive into the JavaScript microbenchmark on MeasureThat.net.
Benchmark Definition: The benchmark is designed to compare the performance of two approaches for creating a new object by copying an existing one:
...
).Object.assign()
.Options Compared:
Object.assign()
:Library:
In both test cases, no external library is used. The Object.assign()
method is a built-in part of the JavaScript language.
Special JS Feature/Syntax: The benchmark uses modern JavaScript syntax features, specifically:
...
): Introduced in ECMAScript 2015 (ES6).\r\nconst finalObject = {\r\n\t...firstObject,\r\n};
): Also introduced in ES6.Other Considerations:
Alternatives: If you want to explore alternative approaches for creating new objects in JavaScript, consider:
Object.create()
and setting properties individually: const finalObject = Object.create(firstObject); finalObject.propertyName = 'value';
Keep in mind that these alternatives might have different performance characteristics and use cases compared to the spread operator and Object.assign()
.