Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSON clone
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs Json Clone
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 Clone
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 Clone
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. **Benchmark Definition** The benchmark is designed to compare three different approaches for creating a deep copy of an object: `lodash.cloneDeep`, `structuredClone` (the native JavaScript method), and `JSON.parse(JSON.stringify())`. **Options Compared** Here are the options being compared: * **Lodash Clone Deep**: Uses the `_.cloneDeep()` function from the Lodash library to create a deep copy of the input object. * **Native Structured Clone**: Uses the `structuredClone` method (introduced in ECMAScript 2020) to create a deep copy of the input object. This is the native JavaScript method for creating copies of objects and arrays. * **JSON Clone**: Uses the `JSON.parse(JSON.stringify())` syntax to create a shallow clone of the input object, and then recursively clones each property to create a deep copy. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash Clone Deep**: + Pros: Easy to use, well-tested, and widely supported. + Cons: Requires an external library (Lodash), which may not be included in all JavaScript environments. * **Native Structured Clone**: + Pros: Native implementation, efficient, and supported by most modern browsers. + Cons: Only available in ECMAScript 2020 and later versions. * **JSON Clone**: + Pros: Lightweight, easy to implement, and widely supported. + Cons: Creates a shallow copy initially, which may not be sufficient for all use cases. Recursively cloning each property can lead to performance issues. **Library and Purpose** The `structuredClone` method is part of the ECMAScript standard (ECMA-404) and was introduced in ECMAScript 2020 as a way to create copies of objects and arrays while preserving their original structure and semantics. It's designed to be more efficient and safe than traditional cloning methods, such as `JSON.parse(JSON.stringify())`. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax being tested in this benchmark. **Other Considerations** When choosing a cloning method, consider the following factors: * **Performance**: If speed is crucial, `structuredClone` might be the best choice. * **Security**: If security is a concern, `structuredClone` can help avoid issues like prototypal inheritance or circular references. * **Compatibility**: If you need to support older JavaScript versions or environments without Lodash, `JSON.parse(JSON.stringify())` or `structuredClone` (if available) might be better options. **Alternatives** Other alternatives for creating deep copies of objects include: * Using a library like Immer, which provides an immutable version of the cloning method. * Implementing a custom cloning function that recursively clones each property of the object. * Using a third-party library like DeepClone or Copy.js.
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-Clone
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs Json.stringify
Comments
Confirm delete:
Do you really want to delete benchmark?