Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs spreading object copy
(version: 0)
Comparing performance of:
Spread vs Object.assign
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
let object = {...{foo: "bar"}}
Object.assign
let object = Object.assign({}, {foo: "bar"})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
80873648.0 Ops/sec
Object.assign
40931416.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Description** The benchmark is comparing two ways to create a copy of an object: using the spread operator (`...`) and using `Object.assign()`. **Options Compared** 1. **Spread Operator (`...`)**: This method creates a new object by copying the properties from an existing object. It's a concise way to create a shallow copy of an object. 2. **Object.assign()**: This method creates a new object by copying the specified properties from one or more source objects. **Pros and Cons** * **Spread Operator (`...`)**: + Pros: Concise, easy to read, and efficient for simple cases. + Cons: Creates a shallow copy, which means it only copies the top-level properties. If the object has nested objects or arrays, they won't be copied recursively. * **Object.assign()**: + Pros: More flexible, can create deep copies by using multiple source objects. + Cons: Less concise and more verbose than the spread operator. **Library and Special JS Feature** There is no specific library being used in this benchmark. However, it's worth noting that the `...` syntax was introduced in ECMAScript 2015 (ES6) as a shorthand for creating an object from an array of key-value pairs or an existing object with spread syntax. **Benchmark Test Cases** The test cases are designed to compare the performance of each method: 1. **Spread Operator (`...`)**: The benchmark creates an object `object` with a single property `foo` set to `"bar"`. It then measures the time it takes to create an array of key-value pairs using the spread operator and assign it to a new variable. 2. **Object.assign()**: The benchmark creates an object `object` with a single property `foo` set to `"bar"`. It then measures the time it takes to use `Object.assign()` to create a copy of the object. **Other Considerations** * The benchmark is designed to test the performance of each method on a specific type of data (a simple object with one property). In more complex scenarios, the results may vary. * The benchmark doesn't account for the overhead of function calls or other JavaScript operations that might affect performance. **Alternative Approaches** If you need to create a deep copy of an object, you can use libraries like Lodash's `cloneDeep()` or Underscore's `clone()`. Alternatively, you can use recursive functions to create a deep copy by copying each nested property individually. For more complex scenarios or performance-critical code, you may want to consider using just-in-time (JIT) compilation or other optimization techniques specific to your JavaScript engine.
Related benchmarks:
JS object copy spread vs assign
JavaScript spread operator vs Object.assign performance for cloning
Object.assign mutation vs spread
object.assign vs spread operator for shallow copying large objects 2
Comments
Confirm delete:
Do you really want to delete benchmark?