Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs deepClone v6
(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: null, 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: null, available: true }, { id: 5, title: 'Another Great Book', price: 13.99, available: false } ]; var clone = null; var hasOwn = Object.prototype.hasOwnProperty; function deepClone (source) { if (source instanceof Date) return new Date(source.getTime()) if (Array.isArray(source)) { const clone = [] let i = source.length while (i--) { const value = source[i] clone[i] = (value !== null && 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] = (value !== null && 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 and explain what's being tested. **Overview** The benchmark is comparing three different methods to create a deep clone of an object: `lodash`'s `cloneDeep`, `JSON.parse(JSON.stringify())`, and a custom implementation called `deepClone v6`. **Options Compared** 1. **Lodash CloneDeep**: The `cloneDeep` function from the Lodash library creates a deep copy of an object, recursively copying all nested properties. 2. **JSON.parse(JSON.stringify())**: This method uses JSON serialization to create a deep copy of an object. It serializes the original object to a string, then deserializes it back to an object. 3. **DeepClone v6**: The custom implementation `deepClone` is a recursive function that creates a deep copy of an object. **Pros and Cons** * **Lodash CloneDeep**: Pros: + Easy to use and maintain + Provides additional features like array cloning, map creation, etc. + Well-tested and maintained by the Lodash community * Cons: None notable * **JSON.parse(JSON.stringify())**: Pros: + Simple and easy to understand + Works for most cases, but has some edge cases (e.g., circular references) * Cons: Can be slow due to the overhead of serializing and deserializing objects. Not suitable for large datasets. * **DeepClone v6**: Pros: + Customizable through its implementation + Could potentially outperform Lodash CloneDeep in specific use cases * Cons: Requires more maintenance and testing effort, as it's a custom implementation. **Library - JSON** The `JSON` library is used for serializing and deserializing objects to/from strings. The `parse()` function takes a string representation of an object (e.g., from `JSON.stringify()`) and returns the original object. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. However, it's worth noting that some browsers may have specific implementations or limitations for certain methods, such as using `Object.assign()` instead of `Array.prototype.push()` to clone arrays. **Other Alternatives** If you need a deep clone, there are other alternatives: * `Array.prototype.slice()` and `Array.prototype.concat()` can be used to create shallow clones of arrays. * The `Array.from()` method can be used to create a shallow clone of an array by mapping over the original array. * The `Spread operator` (`...`) can be used to create a shallow clone of an object by spreading its properties. Keep in mind that these alternatives may not provide the same level of deep cloning as the methods mentioned in this benchmark.
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?