Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON.parse/stringify
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs JSON
Created:
3 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 = structuredClone(MyObject);
JSON
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
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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided JSON represents a benchmark test case that compares three different approaches for creating deep copies of objects in JavaScript: 1. **Lodash `cloneDeep`**: The Lodash library is a popular utility belt for functional programming tasks in JavaScript. `cloneDeep` is a function that creates a deep copy of an object, preserving all nested properties and their respective structures. 2. **Native `structuredClone`**: This is a new JavaScript feature introduced in ECMAScript 2020, which provides a way to create a shallow clone of an object using the `structuredClone` function. Although it's called "shallow," it can also work with nested objects by passing options. 3. **JSON.parse/stringify combination**: The JSON.parse and JSON.stringify methods can be used together to convert a JavaScript object to a string representation, which can then be converted back into an object using the `JSON.parse` method. **Comparison of Approaches:** * **Lodash `cloneDeep`:** * Pros: * Works with nested objects * Preserves object's prototype chain * Easy to use and well-documented * Cons: * Requires the Lodash library, which can add extra overhead * May not be as efficient as native implementation (structuredClone) * **Native `structuredClone`:** * Pros: * Efficient and fast * No additional library dependency * Works with shallow cloning by default, but can also work with nested objects using options * Cons: * Requires modern browsers that support ECMAScript 2020 (structuredClone) * May not be as well-documented or widely understood as Lodash `cloneDeep` * **JSON.parse/stringify combination:** * Pros: * Works with any JavaScript object * Easy to understand and implement * Cons: * Can lead to security issues if the input is not properly sanitized * May result in slower performance due to string parsing **Other Considerations:** * **Security:** When using the JSON.parse/stringify combination, ensure that the input object is properly sanitized to prevent potential security risks. * **Browsers Support:** The native `structuredClone` function requires modern browsers that support ECMAScript 2020. This might limit its applicability in older browsers or environments. **Alternatives:** Other alternatives for deep copying objects include: * Using a library like Lo-Dash, Underscore.js, or Mimic.js * Implementing a custom deep copy function using recursion or iteration Keep in mind that the choice of approach depends on your specific use case, performance requirements, and the level of compatibility you need.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
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?