Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep clone JS
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Native JSON parse
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></script>
Script Preparation code:
var testArray = [{ description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 }, testArray: [{ myName: 'test name', myNumber: 123245 }] },{ description: 'Random description.', testNumber: 123456789, testBoolean: true, testObject: { testString: 'test string', testNumber: 12345 }, testArray: [{ myName: 'test name', myNumber: 123245 }] }]; var testCopy = null;
Tests:
Lodash cloneDeep
testCopy = _.cloneDeep(testArray);
Native JSON parse
testCopy = JSON.parse(JSON.stringify(testArray));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native JSON parse
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 what's being tested in this benchmark. **Test Case 1: Lodash `cloneDeep`** The first test case uses the Lodash library, which is a popular JavaScript utility library. In this specific test, Lodash's `cloneDeep` function is used to create a deep copy of the `testArray`. A deep copy is an exact copy of the original object, including all its properties and child objects. **What's being compared:** In this test case, we're comparing the performance of Lodash's `cloneDeep` function with the native JavaScript method of creating a deep copy using `JSON.parse(JSON.stringify())`. **Pros and Cons:** Lodash's `cloneDeep` has several advantages: * It provides a robust implementation of deep cloning, handling complex data structures like arrays with nested objects. * It's optimized for performance, making it suitable for large datasets. However, Lodash is an external library, which means there's an additional overhead in terms of loading and parsing the code. This can lead to slower startup times and increased memory usage. On the other hand, using `JSON.parse(JSON.stringify())` is a native JavaScript method that creates a shallow copy of an object by serializing it as JSON and then parsing it back into an object. While it's faster than Lodash's implementation, it has limitations: * It only works for simple data structures like primitive values, arrays with primitive values, and objects with primitive values. * It doesn't handle complex data structures like nested objects or cyclic references. **Test Case 2: Native `JSON.parse(JSON.stringify())`** The second test case uses the native JavaScript method of creating a deep copy using `JSON.parse(JSON.stringify())`. This is essentially the same as the first test case, but without the overhead of Lodash's library. **Pros and Cons:** As mentioned earlier, this method has limitations, such as not handling complex data structures or cyclic references. However, it's still faster than Lodash's implementation since it doesn't involve loading an external library. **Other Considerations:** * The benchmark assumes that the input data is identical for both test cases, which may not be true in real-world scenarios. * The benchmark only measures performance and doesn't consider other factors like memory usage or code readability. **Alternatives:** If you're looking for alternative deep cloning methods in JavaScript, some popular options include: * `Array.prototype.slice.call()`: Creates a shallow copy of an array by converting it to a new array using the `slice()` method. * `Object.assign()`: Creates a shallow copy of an object by copying its properties onto a new object using the `assign()` method. * `angular.copy()`: A utility function from the Angular framework that creates a deep copy of an object. Keep in mind that these alternatives have their own limitations and may not handle complex data structures as well as Lodash's `cloneDeep` or the native JavaScript method.
Related benchmarks:
Lodash cloneDeep vs best native cloneDeep option
Lodash cloneDeep vs native cloneDeep - deepclone only
Lodash cloneDeep vs native cloneDeep (copy object)
JAVASCRIPTOBJECT DEEP COPY
Comments
Confirm delete:
Do you really want to delete benchmark?