Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object spread vs assign of another object
(version: 0)
Comparing performance of:
spread two objects vs assign two objects vs assign from empty object
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
spread two objects
const range = (from, to) => { const output = [] for(var x = from; x < to; x++){ output.push(x) } return output } range(0, 10).reduce((acc, num) => { return { ...acc, ...{ [num]: num } } }, {})
assign two objects
const range = (from, to) => { const output = [] for(var x = from; x < to; x++){ output.push(x) } return output } range(0, 10).reduce((acc, num) => { return Object.assign(acc, { [num]: num }) }, {})
assign from empty object
const range = (from, to) => { const output = [] for(var x = from; x < to; x++){ output.push(x) } return output } range(0, 10).reduce((acc, num) => { return Object.assign({}, acc, { [num]: num }) }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread two objects
assign two objects
assign from empty object
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
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of each approach. **Benchmark Definition** The benchmark is comparing three approaches for merging two objects in JavaScript: 1. `Object.assign()` 2. Object spread operator (`...`) 3. Merging from an empty object (`Object.assign({}, ...)` **Options Compared** * **`Object.assign()`**: This method takes multiple source objects and merges them into a target object. It's been a part of the JavaScript standard library since ECMAScript 2015 (ES6). * **Object Spread Operator (`...`)**: Introduced in ES6, this operator allows you to spread the properties of an object onto another object. * **Merging from an empty object (`Object.assign({}, ...)`)**: This approach uses `Object.assign()` with two arguments: an initial value (an empty object) and a source object. The result is merged back into the initial value. **Pros and Cons** * **`Object.assign()`**: + Pros: Simple, widely supported, and reliable. + Cons: Can be slow due to its method invocation overhead, and it only works with objects. * **Object Spread Operator (`...`)**: + Pros: Convenient, expressive, and often faster than `Object.assign()`. + Cons: Requires modern browsers or Node.js versions that support ES6+ syntax. * **Merging from an empty object (`Object.assign({}, ...)`)**: + Pros: Similar performance to using `Object.assign()` directly, but with the added benefit of creating a new object if the initial value is an object (i.e., not null or undefined). + Cons: May be less intuitive than using `Object.assign()` directly. **Library and Special Features** None mentioned in the provided benchmark definition. However, it's worth noting that some libraries, like Lodash, provide additional methods for merging objects, such as `_.merge()`, which might be used in other benchmarks. **Other Considerations** When choosing between these approaches, consider the following: * If you need to merge two objects with similar properties, but want a simple and reliable solution, use `Object.assign()`. * For more concise and expressive code, use the Object Spread Operator (`...`). * When merging from an empty object is necessary (e.g., to avoid overwriting existing properties), consider using `Object.assign({}, ...)`.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
object.assign vs spread to create a copy
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?