const obj = { foo: 1, bar: 2 };
const finalObject = ({ baz: 3, obj })
const obj = { foo: 1, bar: 2 };
const finalObject = Object.assign({baz: 3}, obj);
const obj = { foo: 1, bar: 2 };
Object.assign({}, obj, { baz: 3 });
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Basic object spread | |
Basic assign operator | |
Assign with empty object |
Test name | Executions per second |
---|---|
Basic object spread | 7249307.5 Ops/sec |
Basic assign operator | 2133161.5 Ops/sec |
Assign with empty object | 1288553.6 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript benchmark test case on MeasureThat.net, titled "Spread vs Assign benchmark". The test compares three different approaches to merge two objects in JavaScript: using the spread operator (...
), the assignment operator (=
), and a combination of assigning to an empty object.
Approaches Compared
{ ... }
) to create a new object that includes all properties from obj
, with additional properties added from the { baz: 3, ...obj }
expression.=
) to merge two objects. The syntax is similar to the previous one, but instead of using the spread operator, it assigns obj
directly to a new object created with Object.assign()
.{}
and then uses the assignment operator to merge it with another object.Pros and Cons
Other Considerations
When choosing between these approaches, consider factors like readability, performance, and maintainability. For most use cases, the spread operator is a clear winner in terms of simplicity and conciseness. However, in specific situations where memory efficiency or fast execution are crucial, the assignment operator or Assign with Empty Object might be more suitable.
Libraries Used
None of these test cases explicitly uses any third-party libraries. The JavaScript engine's built-in objects and operators (Object.assign()
, spread operator) are used throughout the benchmark.
Special JS Features/Syntax
The benchmark does not use any special JavaScript features or syntax beyond what is standard in modern JavaScript. It only relies on basic language constructs, making it accessible to a wide range of developers without deep knowledge of JavaScript.