Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object flat copy
(version: 0)
Comparing performance of:
Object assign vs Object spread
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const logging = { maskLogging: true, maskAnalytics: false, disableConversations: true }
Tests:
Object assign
const logging = { maskLogging: true, maskAnalytics: false, disableConversations: true } const myCopy = Object.assign({}, logging)
Object spread
const logging = { maskLogging: true, maskAnalytics: false, disableConversations: true } const myCopy = { ...logging }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object assign
Object spread
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):
I'll explain what's being tested in the provided benchmark. **Benchmark Overview** The test case measures the performance difference between two approaches to create a shallow copy of an object: `Object.assign()` and the spread operator (`{ ... }`). **Options Compared** There are two options compared: 1. **Object.assign()**: This method creates a new object by copying all enumerable own properties from one or more source objects. 2. **Spread Operator (`{ ... }`)**: This syntax creates a new object with the same properties as an existing object, without creating a deep copy. **Pros and Cons** * **Object.assign()**: + Pros: - Widely supported in older browsers and Node.js versions. - Easy to use for simple cases. + Cons: - Can be slower than the spread operator due to the overhead of iterating over properties. - May not work as expected if the source object has non-enumerable properties or is a complex data structure (e.g., arrays, dates). * **Spread Operator (`{ ... }`)**: + Pros: - Faster and more efficient than `Object.assign()` for shallow copies. - Creates a new object with the same properties as the source object, making it easier to work with. + Cons: - Requires support for modern JavaScript features (ECMAScript 2018+). - May not work as expected if the source object has non-enumerable properties or is a complex data structure. **Library and Special JS Feature** There are no external libraries used in this benchmark. However, the spread operator syntax relies on the ECMAScript 2018+ standard, which includes the feature of creating objects using the spread operator (`{ ... }`). **Other Alternatives** If you need to create a shallow copy of an object without using `Object.assign()` or the spread operator, you can use other methods, such as: * Using the `JSON.parse(JSON.stringify(obj))` method, which can be slower than the spread operator. * Implementing your own custom copying logic using a loop or recursion. It's worth noting that if you need to create a deep copy of an object (i.e., recursively copying all nested properties), you'll need to use a different approach altogether.
Related benchmarks:
Assignment of value vs Destructuring an object
Assignment of value vs Destructuring an object v2
destructuring assignment vs assignment single
Assignment of value vs Destructuring an object (direct assign insted of variable )
Comments
Confirm delete:
Do you really want to delete benchmark?