Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs deepClone v8
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs deepClone
Created:
3 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 Books = [ { id: 1, title: 'Great Book', price: 12.99, available: true }, { id: 2, title: 'Another Great Book', price: 11.99, available: false }, { id: 3, title: 'Another Great Book', price: 9.99, available: true }, { id: 4, title: 'Another Great Book', price: 8.99, available: true }, { id: 5, title: 'Another Great Book', price: 13.99, available: false } ]; var clone = null; function deepClone(source) { if (source instanceof Date) return new Date(source.getTime()) if ({}.toString.call(source) === '[object Array]') { const clone = [] let i = source.length while (i--) { const value = source[i] clone[i] = (typeof value === 'object') ? deepClone(value) : value } return clone } const clone = {} for (const key in source) { if ({}.hasOwnProperty.call(source, key)) { const value = source[key] clone[key] = (typeof value === 'object') ? deepClone(value) : value } } return clone }
Tests:
Lodash CloneDeep
clone = _.cloneDeep(Books);
Json Clone
clone = JSON.parse(JSON.stringify(Books));
deepClone
clone = deepClone(Books);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
deepClone
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 break down the provided benchmark definition and explain what is being tested. The benchmark compares three different methods for creating a deep copy of an object: 1. **Lodash `cloneDeep`**: This method uses a recursive approach to create a deep copy of the object. It checks if the source object is an array, and if so, it creates a new array with the same length and recursively clones each element. For non-array objects, it uses a simple property-by-property copying approach. 2. **`JSON.parse(JSON.stringify())`**: This method uses the `JSON.stringify()` function to serialize the original object to a string, and then parses that string back into an object using `JSON.parse()`. The resulting object is not necessarily a deep copy, as it will only clone the top-level properties of the original object. 3. **Custom `deepClone` function**: This method is a custom implementation of a deep copying algorithm. It recursively clones each property of the source object, including arrays and objects. The benchmark compares the performance of these three methods on an array of objects (`Books`). The test case uses the Lodash library to create the `cloneDeep` object. **Pros and Cons:** * **Lodash `cloneDeep`**: + Pros: Fast and efficient for large objects, handles arrays and nested objects correctly. + Cons: May have additional dependencies (e.g., Lodash), not as lightweight as other options. * **`JSON.parse(JSON.stringify())`**: + Pros: Lightweight and easy to implement, works well for simple objects. + Cons: Can be slow for large objects or complex data structures, may not handle cyclic references correctly. * **Custom `deepClone` function**: + Pros: Customizable, can be optimized for specific use cases, handles arrays and nested objects correctly. + Cons: May require more maintenance and testing than other options. The benchmark results show that the custom `deepClone` function performs closest to the Lodash `cloneDeep` method in terms of execution speed. However, the performance difference is relatively small, and the choice of implementation ultimately depends on the specific requirements and constraints of the project. **Other alternatives:** * **`.slice()` and `Array.prototype.slice.call()`**: These methods create a shallow copy of an array by returning a new array with references to the original elements. * **`Object.assign()`**: This method creates a shallow copy of an object by copying its top-level properties to a new object. In general, for simple objects or small datasets, these alternatives may be sufficient. However, for larger datasets or more complex data structures, the custom `deepClone` function or Lodash `cloneDeep` method are likely to be more efficient and reliable.
Related benchmarks:
Lodash deep clone vs JSON.stringfy
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
cloneDeep vs JSON stringify + parse (long arr)
lodash cloneDeep vs json.stringify
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?