Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone
Created:
8 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));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
948196.9 Ops/sec
Json clone
886846.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark compares two approaches to create a deep copy of an object: using the `cloneDeep` function from Lodash and using the `JSON.parse(JSON.stringify())` method. The test case creates a sample object `MyObject` with nested properties, including objects and arrays. **Options Compared** Two options are compared: 1. **Lodash cloneDeep**: This approach uses the `cloneDeep` function from Lodash to create a deep copy of the object. Lodash is a utility library that provides functions for functional programming tasks. 2. **JSON Clone**: This approach uses the `JSON.parse(JSON.stringify())` method, which converts the original object into a JSON string and then parses it back into an object. **Pros and Cons** 1. **Lodash cloneDeep**: * Pros: + Provides a more efficient and flexible way to create deep copies of objects. + Offers additional features like handling circular references. * Cons: + Requires including the Lodash library in your code. 2. **JSON Clone**: * Pros: + Does not require any external libraries. + Can be a simple and straightforward solution for small objects. * Cons: + May not work correctly with complex objects or circular references. + Can be slower than `cloneDeep` due to the overhead of converting to JSON and parsing back. **Library: Lodash** Lodash is a popular utility library that provides functions for functional programming tasks, such as array manipulation, object transformation, and more. The `cloneDeep` function is part of this library and is specifically designed to create deep copies of objects. **Other Considerations** * **Memory Allocation**: When creating a copy of an object using `cloneDeep`, Lodash allocates new memory for the copied object, which can be beneficial if the original object is large and you want to avoid modifying it. * **Caching**: MeasureThat.net may cache the results of previous runs to reduce the execution time. If this happens, your benchmark result might not reflect the actual performance of your specific code. **Special JS Feature or Syntax** None mentioned in the provided benchmark definition. **Alternative Approaches** Other approaches to create deep copies of objects include: 1. **Using `Object.assign()`**: This method creates a shallow copy of an object by copying its enumerable properties. 2. **Using `Array.prototype.slice()` and `JSON.stringify()`**: This approach creates a deep copy of arrays, but may not work correctly with objects. 3. **Using a recursive function**: You can write a custom recursive function to create a deep copy of an object, which would be slower than using `cloneDeep`. Keep in mind that these alternative approaches might not offer the same level of performance or convenience as using Lodash's `cloneDeep` function.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs JSON Clone with Array
lodash cloneDeep vs json.stringify
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?