Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs parse+stringify
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs parse+stringify
Created:
2 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);
parse+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
parse+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 dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares three approaches to create a deep copy of an object: Lodash's `cloneDeep`, the native `structuredClone` function in JavaScript (introduced in ECMAScript 2020), and the combination of `JSON.parse()` and `JSON.stringify()`. **Options Compared** 1. **Lodash cloneDeep**: A library function that creates a deep copy of an object, which is a common use case in programming. 2. **Native structuredClone**: A built-in JavaScript function introduced in ECMAScript 2020, designed to create a deep copy of an object, similar to Lodash's `cloneDeep`. 3. **parse+stringify**: A manual approach using `JSON.parse()` and `JSON.stringify()` to create a deep copy of an object. **Pros and Cons** 1. **Lodash cloneDeep**: * Pros: Well-tested, widely used, and maintained by the Lodash team. * Cons: Adds dependency on the Lodash library, which may not be desirable for all projects. 2. **Native structuredClone**: * Pros: Built-in function, no additional dependencies required, and performance-optimized. * Cons: Requires a modern JavaScript engine that supports ECMAScript 2020 (e.g., Chrome 114+). 3. **parse+stringify**: * Pros: Lightweight and doesn't require any additional dependencies. * Cons: Manual approach can be error-prone, slower than optimized functions like `cloneDeep` or `structuredClone`. **Library Usage** The benchmark uses Lodash's `cloneDeep` function to create a deep copy of the input object. Lodash is a popular JavaScript utility library that provides various high-quality, maintainable, and widely-used functions for tasks such as array manipulation, string manipulation, and more. **Special JS Features/Syntax** The benchmark doesn't use any special JavaScript features or syntax that might be specific to certain browsers or engines. It's designed to be platform-agnostic. **Other Alternatives** For creating deep copies of objects, other alternatives include: * **Array.prototype.slice() + JSON.parse(JSON.stringify())**: A manual approach using `slice()` and the `parse+stringify` combination. * **JSON.parse(JSON.stringify(obj))**: Another manual approach that uses `JSON.parse()` to deserialize the original object's string representation and then reconstructs it. Keep in mind that these alternatives might not be as efficient or reliable as optimized functions like `cloneDeep` or `structuredClone`.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.stringify (small object)
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
Lodash cloneDeep vs json.parse+stringify
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?