Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs Lodash clone 2
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs Lodash 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...' } }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Lodash clone
myCopy = _.clone(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
Lodash clone
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. **Benchmark Overview** The benchmark compares three methods for creating a deep copy of an object: Lodash's `cloneDeep`, `JSON.parse(JSON.stringify())`, and Lodash's `clone`. The test case uses a sample object `MyObject` with nested properties, including a property that contains another object (`jayson`). **Options Compared** The benchmark compares three options: 1. **Lodash cloneDeep**: This method creates a deep copy of the entire object graph using Lodash's `cloneDeep` function. 2. **JSON Clone**: This method uses the `JSON.parse(JSON.stringify())` trick to create a shallow copy of the object, which is then converted to an object using `Object.assign()`. However, this approach does not work correctly for objects with circular references. 3. **Lodash clone**: This method creates a shallow copy of the object using Lodash's `clone` function. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash cloneDeep**: + Pros: Works correctly for deep copying objects, handles circular references. + Cons: Requires Lodash library, may have performance overhead due to cloning the entire object graph. * **JSON Clone**: + Pros: Fast and lightweight, no dependencies required. + Cons: Fails if the original object has circular references, which can cause a stack overflow error. The resulting copy is also not a proper deep copy. * **Lodash clone**: + Pros: Lightweight, easy to use. + Cons: Only creates a shallow copy of the object, does not handle circular references. **Library and Syntax** The benchmark uses Lodash library for its `cloneDeep` and `clone` functions. The `JSON.parse(JSON.stringify())` trick is a built-in JavaScript feature that converts a JSON string to a JavaScript value. **Other Considerations** When dealing with objects that have circular references, using `JSON.parse(JSON.stringify())` can lead to unexpected behavior or errors due to the limitations of this method. In such cases, using a library like Lodash's `cloneDeep` is recommended for creating a deep copy. **Alternatives** If you don't want to use Lodash, you can consider alternative libraries or approaches, such as: * Using `Object.assign()` with the spread operator (`{...obj}`) to create a shallow copy of an object. * Implementing your own recursive function to create a deep copy of an object. However, keep in mind that these alternatives may not handle circular references correctly or be as efficient as using Lodash's `cloneDeep`.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs clone vs spread
Lodash cloneDeep vs JSON Clone with Array
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?