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 / 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.stringify / 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.stringify / parse
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
2782016.8 Ops/sec
Native structuredClone
1171137.5 Ops/sec
JSON.stringify / parse
2591033.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark. **Benchmark Overview** The benchmark compares the performance of three methods for creating a deep copy of an object: `_.cloneDeep` from Lodash, `structuredClone`, and `JSON.stringify/parse`. **Options Compared** 1. **Lodash cloneDeep**: Uses the `_` object (a utility library) to create a deep copy of the input object. 2. **Native structuredClone**: Uses the new `structuredClone()` method in JavaScript (introduced in ECMAScript 2020), which creates a deep copy of an object. 3. **JSON.stringify/parse**: Uses `JSON.stringify()` to convert the object to a JSON string and then parses it back into an object using `JSON.parse()`, which can also create a deep copy. **Pros and Cons** * **Lodash cloneDeep**: + Pros: Well-tested, widely used, and easy to use. + Cons: Adds external dependency (Lodash), may have performance overhead due to its implementation. * **Native structuredClone**: + Pros: New method in JavaScript, designed for deep copying, fast and efficient. + Cons: Only available in modern browsers and Node.js versions that support ECMAScript 2020. * **JSON.stringify/parse**: + Pros: Fast and lightweight, no external dependencies required. + Cons: May have issues with circular references or other edge cases, can be slower than the native `structuredClone` method. **Library and Purpose** The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for various tasks, including functional programming, data manipulation, and more. In this benchmark, `_` object is used to access the `cloneDeep` function, which creates a deep copy of an object. **Special JS Feature or Syntax** The `structuredClone()` method is a new addition to JavaScript (ECMAScript 2020) that creates a deep copy of an object without cloning its prototype chain. This method is designed for creating exact copies of objects, including their inner structures and properties. **Other Alternatives** For creating a deep copy of an object in the absence of `structuredClone()`, other approaches include: * Using the `_` object's `cloneDeep()` method from Lodash or similar libraries. * Implementing a custom deep copying function using recursive functions or other techniques. * Using libraries like Immutable.js, which provides a way to create immutable copies of objects. In summary, this benchmark compares the performance of three methods for creating a deep copy of an object: `_.cloneDeep` from Lodash, `structuredClone`, and `JSON.stringify/parse`. The native `structuredClone()` method is recommended due to its speed and efficiency, but may not be suitable for older browsers or Node.js versions.
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-JSON
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?