Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON Stringify/Parse
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs JSON Stringify
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 Stringify
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 Stringify
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, along with the pros and cons of each approach. **Benchmark Overview** The benchmark compares the performance of three methods for creating a deep copy of an object: 1. `structuredClone` (Native) 2. `_.cloneDeep` from Lodash 3. `JSON.parse(JSON.stringify())` **Library: structuredClone** `structuredClone` is a relatively new API introduced in ECMAScript 2020, which allows creating a deep clone of an object without using the `JSON.parse()`/`JSON.stringify()` method. It's designed to be more efficient and safer than the latter approach. Pros: * More efficient than `JSON.parse(JSON.stringify())` * Safer, as it doesn't expose the cloned object's internal structure Cons: * Limited browser support (only Chrome 106+ and Firefox 93+, others are not yet supported) * May have performance differences depending on the specific use case **Library: Lodash cloneDeep** `.cloneDeep` is a popular utility function in the Lodash library that creates a deep copy of an object. Pros: * Wide browser support (works in most modern browsers) * Well-maintained and widely tested * Easy to use and integrate into existing codebases Cons: * May be slower than `structuredClone` due to its overhead * Requires an external dependency (Lodash) **Library: JSON.parse(JSON.stringify())** The `JSON.parse(JSON.stringify())` method is a simple but inefficient way to create a deep copy of an object. Pros: None significant, as it's known for being slow and not recommended for production use. Cons: * Inefficient, especially for large objects * May expose the cloned object's internal structure (e.g., array indices) * Limited browser support (works in most modern browsers) **Special JS feature: None** There are no special JavaScript features or syntax used in this benchmark. The code only uses standard ECMAScript features. **Other alternatives** If you need to create a deep copy of an object, here are some other alternatives: * `Object.assign(null, obj)`: This method creates a shallow copy of the object, which might not be suitable for all use cases. * `Array.prototype.slice()`: This method can be used to create a shallow copy of arrays. * Custom implementation using recursion or iteration: These approaches can be highly efficient but require more boilerplate code. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific requirements, performance constraints, and personal preference.
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 Json.stringify
lodash clonedeep vs json.parse(stringify()) vs structuredClone
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?