Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DeepClone vs Assign
(version: 0)
Testing deepClone vs object.Assign
Comparing performance of:
Deep Clone vs Assign Shallow
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
Deep Clone
myCopy = _.cloneDeep(MyObject);
Assign Shallow
myCopy = Object.assign({}, MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Deep Clone
Assign Shallow
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Deep Clone
2878997.0 Ops/sec
Assign Shallow
7325137.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark and the options being compared. **Benchmark Definition** The website `MeasureThat.net` provides a JSON definition for a JavaScript microbenchmark. In this case, we have two individual test cases: 1. **Deep Clone**: Testing the use of the `_cloneDeep()` function from the Lodash library to create a deep copy of an object. 2. **Assign Shallow**: Testing the use of `Object.assign()` to shallow-copy an object. **Options being compared** The benchmark is comparing two approaches for creating copies of objects: 1. **Deep Clone**: Using the `_cloneDeep()` function from Lodash, which recursively creates a new copy of the entire object. 2. **Assign Shallow**: Using `Object.assign()` with an empty object (`{}`) as the target, which only shallow-copies the properties of the original object. **Pros and Cons** * **Deep Clone**: + Pros: Recursively creates a complete copy of the object, preserving all nested properties. + Cons: Can be slower and more memory-intensive than shallow copying, especially for large objects. * **Assign Shallow**: + Pros: Faster and more memory-efficient than deep cloning, as it only copies the top-level properties. + Cons: May not preserve nested properties or relationships between objects. **Lodash library** The `_cloneDeep()` function is part of the Lodash library, which provides a set of utility functions for working with JavaScript data structures. The purpose of this function is to recursively create a deep copy of an object, ensuring that all nested properties are preserved. **Browser and Device information** The benchmark results show information about the browser used (Safari 18) and device platform (Desktop). This information can be useful for identifying potential issues with running the benchmark on certain browsers or devices. **Other alternatives** If you wanted to create a deep copy of an object, other alternatives besides Lodash's `_cloneDeep()` function include: * Using the `JSON.parse(JSON.stringify(obj))` method, which serializes the object and then parses it back into a new object. * Implementing your own recursive function to create a deep copy of the object. However, these alternatives may not be as efficient or robust as Lodash's `_cloneDeep()` function.
Related benchmarks:
Lodash deep clone vs JSON.stringfy
Lodash cloneDeep vs clone vs spread
Lodash deep clone (v4.17.20) vs JSON.stringfy
Lodash clone VS spread operator shallow
Fair Lodash deep clone vs Spread Clone
Comments
Confirm delete:
Do you really want to delete benchmark?