Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON PARSE Test
(version: 1)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs JSON String parse
Created:
2 years ago
by:
Registered User
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);
Native structuredClone
myCopy = structuredClone(MyObject);
JSON String parse
myCopy = JSON.parse(JSON.stringify(MyObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native structuredClone
JSON String parse
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, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three methods to create a deep copy of an object: `_.cloneDeep` from Lodash, `structuredClone` (a native JavaScript method introduced in ES2022), and `JSON.parse(JSON.stringify(MyObject))`. **Test Cases** 1. **Lodash cloneDeep**: This test uses the `_.cloneDeep` function from Lodash to create a deep copy of the `MyObject`. `_` is an alias for the Lodash library, which provides utility functions for functional programming. 2. **Native structuredClone**: This test uses the `structuredClone` method (introduced in ES2022) to create a deep copy of the `MyObject`. 3. **JSON String parse**: This test uses the `JSON.parse(JSON.stringify(MyObject))` syntax to create a deep copy of the `MyObject`. Note that this method does not support cyclic references, which can lead to incorrect results. **Comparison** The benchmark compares the performance of these three methods across different browsers and execution rates. The results show that: * Lodash's `_.cloneDeep` is generally faster than the native `structuredClone` method. * The `JSON String parse` method is slower and less reliable, especially with large input sizes. **Pros and Cons** 1. **Lodash cloneDeep**: Pros: * Highly optimized and performant. * Supports cyclic references out of the box. Cons: * Requires Lodash library inclusion. 2. **Native structuredClone**: Pros: + Native implementation, no additional library required. + Supports cyclic references. Cons: + Limited support in older browsers (pre-ES2022). 3. **JSON String parse**: Pros: None significant. **Other Considerations** * The `structuredClone` method is designed to work with structured data formats like JSON and XML. It's optimized for performance and safety, but might not be suitable for all use cases. * Lodash's `_.cloneDeep` is a well-maintained and widely adopted solution, but it may have performance overhead due to its complexity. **Alternatives** If you don't want to use Lodash or rely on the native `structuredClone` method, you can consider: 1. **lodash-es**: A lightweight version of Lodash that's compatible with modern browsers. 2. **JSON.parse(JSON.stringify()) + cyclic references workarounds**: If you need to support older browsers, you might consider implementing a workaround for cyclic references using techniques like JSON string manipulation or caching. Keep in mind that these alternatives may introduce additional complexity and performance overhead. I hope this explanation helps!
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs json.parse+stringify
Lodash cloneDeep vs structuredClone vs Json.stringify
lodash clonedeep vs json.parse(stringify()) vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?