Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs spread operator.
(version: 0)
Comparing performance of:
Object.assign vs spread operator
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Object.assign
var params = { b:"hello", c: true, d:7 }; var other = Object.assign({ a: 2 }, params);
spread operator
var params = { b:"hello", c: true, d:7 }; var other = { a: 2, ...params };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways to create a new object by combining existing properties: `Object.assign()` and the spread syntax (`...`). **Options Compared:** * **`Object.assign()`:** This is a built-in JavaScript function that creates a new object by copying the values of all enumerable own properties from one or more source objects to a target object. * Code Example: `Object.assign({ a: 2 }, { b: "hello", c: true, d: 7 });` * **Spread Syntax (`...`):** Introduced in ES6, this syntax allows you to expand the contents of an iterable (like an object) into individual elements within another iterable. * Code Example: `{ a: 2, ...{ b: "hello", c: true, d: 7 } };` **Pros/Cons:** | Method | Pros | Cons | | :----- | :------------------------------------------------------- | :--------------------------------------------- | | `Object.assign()` | - Well-established and widely understood | - Can be more verbose compared to spread syntax | | Spread Syntax (`...`) | - More concise and modern | - Requires understanding of ES6 syntax | **Other Considerations:** * **Performance:** In this specific benchmark, `Object.assign()` performs slightly better than the spread operator, but performance differences can vary depending on the size and complexity of the objects involved. * **Flexibility:** `Object.assign()` allows you to merge properties from multiple source objects into a single target object. The spread syntax is primarily used for expanding properties from a single object. **Alternatives:** While `Object.assign()` and the spread operator are common methods, there are other ways to achieve similar results in JavaScript: * **Using property assignment (`{ ... }`):** Manually assigning properties one by one can be useful for specific control over which properties are copied.
Related benchmarks:
toFixed -> Number vs Math.round
toFixed() vs Math.round().toString()
toFixed() vs String(Math.floor()
toFixed vs Math.round() with numbers222
Instanceof VS toString for date comparison when using objects
Comments
Confirm delete:
Do you really want to delete benchmark?