const obj = { one: 'one' }
const res = Object.assign({}, { toString: function() { return this.one; }});
const res = {obj, toString: function() { return this.one; }};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.assign | |
Destructuring |
Test name | Executions per second |
---|---|
Object.assign | 19201438.0 Ops/sec |
Destructuring | 18080626.0 Ops/sec |
The benchmark being tested compares two JavaScript techniques for creating new objects with some properties derived from another object: Object.assign
and destructuring with the spread operator. Here’s a breakdown of each method being compared, along with pros, cons, and additional considerations.
Object.assign:
Benchmark Definition:
const res = Object.assign({}, { toString: function() { return this.one; }});
Description:
Object.assign
takes a target object (in this case, an empty object {}
) and one or more source objects, copying the properties from the source(s) to the target. Here, it’s used to create a new object with a toString
method.Pros:
Cons:
Destructuring with Spread Operator:
Benchmark Definition:
const res = {...obj, toString: function() { return this.one; }};
Description:
...
) allows an iterable (like an object) to be expanded into individual elements. Here, it is used to create a new object by spreading the properties of obj
and adding a toString
method.Pros:
Cons:
Object.assign
, it also performs a shallow copy and does not handle merging of deep structures.Both techniques have been tested in the same environment using Firefox on a Mac system, yielding the following execution per second rates:
Object.assign
: 19,201,438 executions per secondDestructuring
: 18,080,626 executions per secondPerformance: In this specific benchmark, Object.assign
showed a marginally better performance compared to destructuring with the spread operator. However, performance differences might vary in different contexts or environments.
Readability & Maintainability: The spread operator may be preferred for readability, especially in more complex object structures. Code maintainability often outweighs micro-optimizations in performance.
Compatibility: While both methods are widely supported in modern JavaScript environments, developers should be mindful of the need for transpilation or polyfills when working in older environments.
Object.create()
method: This can be used for creating a new object with the specified prototype object and properties. It's effective for a more nuanced control over object inheritance.
Manual Copying: Developers could also create new objects using traditional assignment or loops to control exactly what gets copied, allowing for more fine-tuned performance optimizations if necessary.
Libraries: Libraries like Lodash offer utility functions such as _.assign
or _.merge
which can provide additional features, including deep copying and merging of properties.
Ultimately, the choice between Object.assign
and destructuring may come down to code style preferences, specific use cases, performance implications relevant to the project, and available browser support in the target environments.