Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs fastDeepClone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs Fast deep clone
Created:
4 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 = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; function deepClone(preObj) { let newObj, value, key; if (typeof preObj !== "object" || preObj === null) { return preObj; } newObj = Array.isArray(preObj) ? [] : {} for (key in preObj) { value = preObj[key]; newObj[key] = deepClone(value); } return newObj } var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Fast deep clone
deepClone(MyObject)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Fast deep clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
590857.6 Ops/sec
Json clone
1617296.8 Ops/sec
Fast deep clone
10325004.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Test Cases** The test cases compare three different approaches to create a deep copy of an object: `Lodash cloneDeep`, `JSON Clone`, and `Fast deep clone`. 1. **Lodash cloneDeep**: This approach uses the `lodash` library's `cloneDeep` function to create a deep copy of the object. 2. **JSON Clone**: This approach uses the built-in `JSON.stringify()` method to serialize the object, convert it back to a JSON string using `JSON.parse()`, and then parse the resulting string back into an object. 3. **Fast deep clone**: This approach is implemented as a simple recursive function called `deepClone` that creates a new object with the same properties as the original object. **Comparison** The test cases measure the execution time of each approach on the provided object, which contains nested objects and arrays. **Pros and Cons** 1. **Lodash cloneDeep**: Pros: * Efficiently creates deep copies using optimized algorithms. * Handles complex data structures like arrays and objects with ease. * Well-tested and maintained library. Cons: * Requires an additional dependency (`lodash`). * May have performance overhead due to the library's complexity. 2. **JSON Clone**: Pros: * Lightweight, no external dependencies required. * Easy to implement, as it only uses built-in methods. Cons: * Can lead to slower execution times due to string serialization and parsing. * Does not handle complex data structures like arrays well. 3. **Fast deep clone**: Pros: * Simple, easy to understand implementation. * Handles basic data structures like objects and arrays without issues. Cons: * Recursive function can cause stack overflows for large input sizes. * Not optimized for performance or error handling. **Library Explanation** The `lodash` library is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array and object manipulation, and more. The `cloneDeep` function in particular is designed to create deep copies of complex data structures, making it efficient and convenient for this benchmark. **Special JS Feature or Syntax** The test case uses the `let` keyword with the `const` property, which is a shorthand way of declaring variables in JavaScript. This syntax is supported in modern browsers and Node.js environments. **Other Alternatives** For creating deep copies of objects, other alternatives include: 1. **Immutable.js**: A library that provides immutable data structures and functions for creating copies. 2. **immer**: A lightweight library for creating immutable copies of objects. 3. ** plain object assignment**: For simple cases, assigning properties directly to a new object can be sufficient. These alternatives may have different trade-offs in terms of performance, memory usage, or complexity, depending on the specific use case.
Related benchmarks:
Lodash deep clone vs JSON.stringfy
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?