Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash structuredClone vs JSON Clone vs _.cloneDeep
(version: 0)
Comparing performance of:
structuredClone vs Json clone vs Lodash cloneDeep
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:
structuredClone
myCopy = structuredClone(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
structuredClone
Json clone
Lodash cloneDeep
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 provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing three approaches to create a deep copy of an object: `structuredClone` (from the WebAssembly Text Format specification), `JSON.parse(JSON.stringify())`, and `_cloneDeep` from the Lodash library. **Options Compared** 1. **structuredClone**: This method creates a deep copy of an object by serializing it into a WebAssembly binary format, which is then deserialized back into a JavaScript object. 2. **JSON.parse(JSON.stringify())**: This method uses the `JSON.stringify()` function to serialize the object and convert it into a JSON string. It then parses this string using `JSON.parse()` to create a new object that's a copy of the original. **Pros and Cons** * **structuredClone**: Pros: efficient, reliable, and secure (since it serializes the data in a binary format). Cons: may not work well with circular references or complex data structures. * **JSON.parse(JSON.stringify())**: Pros: widely supported, easy to implement. Cons: can lead to infinite loops if there are circular references in the object, and it's not as efficient as `structuredClone`. **Lodash Clone Deep** The Lodash library provides a `cloneDeep()` function that creates a deep copy of an object using a recursive approach. Pros: efficient, handles circular references well. Cons: depends on the Lodash library being available. **Other Considerations** * The benchmark uses a specific example object (`MyObject`) with nested properties and arrays to test each approach. * The `structuredClone` method is tested in its raw form without any additional modifications or optimizations. **Library Used** The benchmark uses the Lodash library for the `_cloneDeep` function, which is included via an external script tag (`<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>`). **Special JS Feature/Syntax** None mentioned. Now, let's look at some alternative approaches: * **Manual Deep Copy**: You can implement a custom function to create a deep copy of an object using recursion or iteration. * **Object.assign()**: This method creates a shallow copy of an object by assigning its properties to a new object. However, it doesn't handle nested objects properly and will only create a copy of the top-level properties. Some other alternatives to `structuredClone` include: * **JSON.stringify() + JSON.parse()`: While this approach can work, it's not as efficient as `structuredClone` and may lead to infinite loops if there are circular references. * **for...in loop**: This approach involves iterating over the object's properties using a `for...in` loop and creating new objects for each property. However, it can be cumbersome to implement and may not handle complex data structures well. Keep in mind that this is just an overview of the benchmark, and there are many other factors that could affect the performance and correctness of these approaches in different scenarios.
Related benchmarks:
Lodash cloneDeep vs JSON stringify vs structuredClone
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(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?