Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy
Created:
2 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 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...' } }; var myCopy = null; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
recursiveDeepCopy
myCopy = recursiveDeepCopy(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
recursiveDeepCopy
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three approaches to create a deep copy of an object: `_.cloneDeep` from Lodash, `JSON.parse(JSON.stringify())`, and a custom recursive function called `recursiveDeepCopy`. The benchmark is designed to test which approach is the fastest for creating deep copies. **Test Cases** 1. **Lodash CloneDeep**: This test case uses the `_` (Lodash) utility library to create a deep copy of the input object using the `_.cloneDeep()` method. 2. **JSON Clone**: This test case uses the built-in JavaScript function `JSON.parse(JSON.stringify())` to create a deep copy of the input object. 3. **recursiveDeepCopy**: This test case uses a custom recursive function to create a deep copy of the input object. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides a wide range of functions and helpers for tasks like array manipulation, object manipulation, and more. The `_` variable in the benchmark refers to the entire Lodash library, which is imported using the `https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js` URL. **JSON.parse(JSON.stringify())** The `JSON.parse(JSON.stringify())` approach uses the built-in JSON serialization and deserialization functions in JavaScript to create a deep copy of the input object. This method is simple but can be slower than other approaches, especially for large objects with complex relationships. **recursiveDeepCopy** The custom `recursiveDeepCopy` function uses recursion to create a deep copy of the input object. This approach is more explicit and allows for fine-grained control over the copying process. However, it may be slower due to the overhead of recursive function calls. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **_.cloneDeep**: Fast, reliable, and widely supported. + Pros: Fast, easy to use, and well-tested. + Cons: May not work as expected for very large or complex objects. * **JSON.parse(JSON.stringify())**: Simple but potentially slow. + Pros: Easy to implement, works well for simple objects. + Cons: May be slower than other approaches, especially for large objects. * **recursiveDeepCopy**: More explicit but may be slower. + Pros: Fine-grained control over the copying process. + Cons: May be slower due to recursive function calls. **Other Alternatives** If you're looking for alternative deep copy libraries or functions in JavaScript, here are a few options: 1. **lodash.cloneDeep()**: Another implementation of the `_.cloneDeep()` method from Lodash. 2. **JSON.stringify() + JSON.parse()**: A simple but potentially slower approach using only built-in JavaScript functions. 3. **Array.prototype.slice() + Array.prototype.concat()**: An array-based approach that can be more efficient than recursive copying methods. Note that the choice of deep copy library or function ultimately depends on your specific use case and performance requirements.
Related benchmarks:
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Comments
Confirm delete:
Do you really want to delete benchmark?