Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash clone vs cloneDeep
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Lodash clone vs new Object vs Custom deep spread vs Object assign vs Custom deep assign
Created:
6 years 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, myDate: new Date(), myDate2: new Date(), myDate3: new Date(), myDate4: new Date(), myDate5: new Date(), myDate6: new Date(), myDate7: new Date(), myDate8: new Date(), myDate9: new Date(), myDate10: new Date(), jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson2: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson3: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson4: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson5: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson6: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson7: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, jayson8: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Lodash clone
myCopy = _.clone(MyObject);
new Object
myCopy = {...MyObject}
Custom deep spread
myCopy = {...MyObject} myCopy.jayson = {...myCopy.jayson} myCopy.jayson2 = {...myCopy.jayson2} myCopy.jayson3 = {...myCopy.jayson3} myCopy.jayson4 = {...myCopy.jayson4} myCopy.jayson5 = {...myCopy.jayson5} myCopy.jayson6 = {...myCopy.jayson6} myCopy.jayson7 = {...myCopy.jayson7} myCopy.jayson7 = {...myCopy.jayson8}
Object assign
myCopy = Object.assign({}, MyObject)
Custom deep assign
myCopy = Object.assign({}, MyObject) myCopy.jayson = Object.assign({}, myCopy.jayson) myCopy.jayson2 = Object.assign({}, myCopy.jayson2) myCopy.jayson3 = Object.assign({}, myCopy.jayson3) myCopy.jayson4 = Object.assign({}, myCopy.jayson4) myCopy.jayson5 = Object.assign({}, myCopy.jayson5) myCopy.jayson6 = Object.assign({}, myCopy.jayson6) myCopy.jayson7 = Object.assign({}, myCopy.jayson7) myCopy.jayson7 = Object.assign({}, myCopy.jayson8)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Lodash clone
new Object
Custom deep spread
Object assign
Custom deep assign
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net! **Benchmark Definition JSON** The provided JSON defines a benchmark test suite that compares the performance of different methods for creating deep copies of objects in JavaScript. **Methods being compared** 1. `_.cloneDeep(MyObject)`: A function from the Lodash library that creates a deep copy of an object. 2. `_.clone(MyObject)`: Another function from the same Lodash library, but it only creates a shallow copy (i.e., a copy of the top-level properties). 3. `new Object` syntax: Creating a new object using the `new` keyword and passing an object literal as its constructor argument. 4. Custom deep spread (`myCopy = mySource; myCopy.$set(specificProperty, value)`): This method uses the `$set()` function from Lodash to update specific properties of the copy object. 5. Custom deep assign (`Object.assign({}, MyObject)`): Using `Object.assign()` to create a new object and then updating its properties with the ones from `MyObject`. **Performance differences** The benchmark results show that: * `new Object` is the fastest method, executing approximately 28% of the executions per second. * The custom deep spread method is slower than the Lodash functions but faster than the custom deep assign method. * The Lodash functions (`_.cloneDeep()` and `_clone()`) are relatively close in performance to each other, with `_.cloneDeep()` being slightly faster. **Implications** When working with complex data structures or objects that contain nested arrays, objects, or other data types, choosing the right deep copy method can significantly impact performance. In general: * If you need a shallow copy of an object, `_clone()` might be sufficient. * For a deep copy, consider using either `_.cloneDeep()`, which is faster and more comprehensive than custom methods. * Custom deep spread or assign methods can be useful when working with specific properties or data structures that require more control over the copying process. Keep in mind that these results are based on Chrome 74 and Windows 10. Other browsers, operating systems, or JavaScript versions may exhibit different performance characteristics. I hope this analysis helps you better understand the nuances of creating deep copies in JavaScript!
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs JSON parse
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?