Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs JSONparse
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs json.parse
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.parse
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.parse
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 benchmark test cases and explanations. **Benchmark Overview** The benchmark compares three methods for creating a deep copy of an object in JavaScript: 1. Lodash's `cloneDeep` function 2. The native `structuredClone` function (available in modern browsers) 3. The `JSON.parse(JSON.stringify())` method **Options Compared** * **Lodash's `cloneDeep`**: This is a custom implementation of deep cloning using the Lodash library. + Pros: Easy to use, provides more features than just deep copying (e.g., handling circular references). + Cons: Requires an additional dependency (the Lodash library), may have performance overhead due to its implementation. * **Native `structuredClone`**: This is a modern browser function that creates a deep copy of an object. + Pros: Fast, efficient, and lightweight, as it's implemented in the browser's engine. + Cons: Not widely supported (only available in modern browsers), may have performance issues if not optimized. * **`JSON.parse(JSON.stringify())`**: This method uses the `JSON.stringify()` function to serialize the object and then parses the resulting string back into an object. + Pros: Fast, lightweight, and widely supported (available in all browsers). + Cons: May not handle circular references correctly, can be slower than native `structuredClone` for large objects. **Library and Functionality** * **Lodash's `cloneDeep`**: Lodash is a popular JavaScript utility library that provides a variety of functions for working with data structures. `cloneDeep` is a specific function that creates a deep copy of an object. + Purpose: To provide a convenient way to create a deep copy of objects, handling circular references and other edge cases. **Special JS Feature or Syntax** * **Native `structuredClone`**: This function was introduced in ECMAScript 2020 (ES2020) as part of the WebAssembly binary format. It's designed to create a stable, efficient, and secure way to copy objects. + Purpose: To provide a native, optimized way to create deep copies of objects, with features like handling circular references and memoization. **Benchmark Preparation Code** The benchmark preparation code creates an object `MyObject` with some properties (e.g., `description`, `myNumber`, `myBoolean`, and `jayson`) and then creates a null reference `myCopy`. This setup is used to test each cloning method on the same initial object. **Alternative Cloning Methods** There are other cloning methods available in JavaScript, including: * **`Object.assign()`**: Creates a shallow copy of an object. * **`Array.prototype.slice()`**: Creates a shallow copy of an array. * **`JSON.parse(JSON.stringify())`**: As mentioned earlier, this method creates a deep copy of an object by serializing it and then parsing the resulting string back into an object.
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?