Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone (Number, Boolean, Object, Array, String, Null)
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone
Created:
5 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 = { string: 'Creates a deep copy of source, which should be an object or an array.', number: 1234567890, boolean: true, nullable: null, json: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' }, array: [ { id: 5, blocked: false, avatar: null, user: { firstName: 'Alan', lastName: 'Walker' }, }, { id: 5688, blocked: false, avatar: null, user: { firstName: 'Anna', lastName: 'Wanderwood' }, }, { id: 80043, blocked: true, avatar: 'image.jpg', user: { firstName: 'Serj', lastName: 'Cooper' }, }, ] }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
386460.7 Ops/sec
Json clone
455773.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring JavaScript performance is a complex task, and MeasuratThat.net provides a valuable platform for comparing different approaches. **Benchmark Definition** The provided benchmark compares the execution times of two methods to create a deep copy of an object: 1. `_.cloneDeep(MyObject)` (Lodash cloneDeep) 2. `JSON.parse(JSON.stringify(MyObject))` (JSON clone) **Comparison of options** Both methods are used to create a deep copy of the input object, but they have different approaches. * **Lodash cloneDeep**: This method uses a recursive approach to create a deep copy of the object. It traverses the object's properties and creates new objects with identical structures. + Pros: Can handle complex objects with nested arrays and objects. + Cons: May be slower for very large objects due to the overhead of recursive function calls. * **JSON clone**: This method uses `JSON.stringify()` and `JSON.parse()` to serialize the object to a string, which is then parsed back into an object. This approach can create a shallow copy, which may not work as expected for deeply nested objects. + Pros: Can be faster for small to medium-sized objects due to the simplicity of the approach. + Cons: May not work correctly for complex objects with nested arrays and objects. **Other considerations** * **Object structure**: The choice of method depends on the object's structure. For simple objects, `JSON clone` may be sufficient. However, for more complex objects, `Lodash cloneDeep` is recommended. * **Performance**: If performance is a critical concern, benchmarking both methods is essential to determine which one performs better in your specific use case. **Library and purpose** * Lodash: A popular JavaScript library that provides utility functions for functional programming, including the `cloneDeep()` method. + Purpose: To provide a simple and efficient way to create deep copies of objects. **Special JS feature or syntax** None mentioned. Both methods are standard JavaScript approaches. **Alternatives** Other alternatives for creating deep copies of objects include: * Using libraries like `immer` or `copy-deep` * Implementing a custom recursive function * Using a third-party library like `deepcopy` Keep in mind that the choice of method depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs JSON Clone with Array
cloneDeep vs JSON stringify + parse (long arr)
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?