Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs deepClone v4
(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: null, 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 === null) return null 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] = (typeof value === 'object') ? deepClone(source[i]) : value } return clone } const clone = {} for (const key in source) { if (source.hasOwnProperty(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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Overview** The benchmark tests three different methods for deep cloning an array of objects: `lodash`'s `cloneDeep`, JSON parsing and stringifying, and a custom implementation called `deepClone`. The test creates an array of book objects with nested properties and measures the execution time of each method. **Options Compared** 1. **Lodash CloneDeep**: This method uses Lodash's `cloneDeep` function to create a deep copy of the input array. * Pros: Efficient, widely used, and well-maintained. * Cons: Requires an external library (Lodash). 2. **JSON Parse/Stringify**: This method uses JSON parsing and stringifying to create a deep copy of the input array. * Pros: Lightweight, no external dependencies required. * Cons: Can be slower due to the overhead of parsing and stringifying JSON. 3. **DeepClone (custom)**: This is a custom implementation of a deep clone function. * Pros: No external dependencies required, can be optimized for performance. * Cons: Requires manual implementation and testing. **Library Used** The `lodash` library is used in the "Lodash CloneDeep" test case. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and object manipulation. In this case, `cloneDeep` is used to create a deep copy of the input array. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in these benchmark tests. They focus on comparing the performance of different cloning methods. **Other Considerations** * **Memory Allocation**: The benchmark tests assume that memory allocation for each clone operation is expensive. This may not be the case in practice, depending on the specific use case and system configuration. * **Garbage Collection**: The benchmark tests do not consider the impact of garbage collection on the performance of the cloning methods. **Alternatives** If you're looking for alternative cloning libraries or implementations, some options include: 1. `Array.prototype.slice()`: This method creates a shallow copy of an array by slicing it. 2. `Array.prototype.concat()`: This method creates a new array with elements from the original array and other arrays. 3. `JSON.parse(JSON.stringify())`: This method creates a deep copy of an object by parsing its string representation. However, keep in mind that these alternatives may not be as efficient or robust as the methods tested in the benchmark.
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
cloneDeep vs JSON stringify + parse (long arr)
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?