Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash _.cloneDeep vs json.parse(stringify())
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/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;
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
1551016.1 Ops/sec
Json Clone
1088538.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested in the benchmark. **Benchmark Definition** The benchmark is comparing two approaches to create a deep copy of an object: 1. **Lodash `.cloneDeep`**: This method clones a value by recursively creating new objects or arrays, depending on the original data type. 2. **JSON parsing and stringifying**: This approach uses the `JSON.parse()` and `JSON.stringify()` methods to convert the object to a JSON string and then parse it back into an object. **Options Compared** The benchmark is comparing two options: * Lodash `.cloneDeep` * JSON parsing and stringifying **Pros and Cons of Each Approach:** 1. **Lodash `.cloneDeep`** * Pros: + Can handle complex data structures, such as arrays and objects with nested properties. + Returns a new object that is independent of the original. + Often faster than manual JSON parsing and stringifying. * Cons: + Requires the Lodash library to be included in the test environment. + May have performance overhead due to the library's complexity. 2. **JSON parsing and stringifying** * Pros: + Lightweight and easy to implement. + Does not require any external libraries. * Cons: + Can lead to shallow copies if the original object is not properly serialized. + May have performance overhead due to the multiple steps involved. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for data manipulation, string manipulation, and more. The `.cloneDeep` method is one of its most useful functions, allowing developers to create deep copies of complex data structures. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntaxes used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * **`Object.assign()`**: This method can be used to create a shallow copy of an object. * **`Array.prototype.slice()` and `JSON.stringify()`**: These methods can be used together to create a deep copy of an array or an object with arrays as values. * **`for...in` loop and JSON string generation**: A manual implementation using a `for...in` loop to iterate over the object's properties and generate a JSON string. These alternatives may have different performance characteristics, complexity, or ease of use compared to the Lodash `.cloneDeep` method and the JSON parsing and stringifying approach.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON.parse(JSON.stringify())
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?