Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs json.parse+stringify
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone
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.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 = 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
Native structuredClone
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
gemma2:9b
, generated one year ago):
This benchmark tests two different approaches to creating a **deep copy** of an object in JavaScript: **1. Lodash's `cloneDeep` function:** This function, from the Lodash library, is designed specifically for creating deep copies of objects and arrays, handling nested structures correctly. * **Pros:** Simple to use, handles complex nested structures efficiently. * **Cons:** Requires including the Lodash library in your project, potentially adding extra size. **2. Native `JSON.parse` and `JSON.stringify`:** This approach serializes the object into a JSON string using `JSON.stringify`, then parses it back into a new object with `JSON.parse`. While this method doesn't directly create a "deep copy", it effectively achieves the same result due to JavaScript's object-copying behavior. * **Pros:** No external library dependency, potentially faster in simple cases. * **Cons:** Can be less efficient for deeply nested objects compared to specialized libraries like Lodash. **Considerations:** * **Performance:** The benchmark results show that `_.cloneDeep` is significantly faster than the native approach in this case. This likely reflects the optimized nature of Lodash's implementation for deep copying. * **Use Cases:** For simple object copies, the native method might be sufficient. However, for complex projects with frequent deep cloning needs, using Lodash (`_.cloneDeep`) often leads to better performance and maintainability. **Alternatives:** While not directly compared in this benchmark: * Other libraries like `ramda` also provide efficient deep-copying functions. * JavaScript's `structuredClone()` method offers a built-in way to create deep copies, though its performance might vary depending on the object structure and browser implementation.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with Array
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs JSON parse
Lodash cloneDeep vs structuredClone vs Json.stringify
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?