Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone vs Fast Copy vs chatGptDeepClone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Native structuredClone vs recursiveDeepCopy vs Json Clone vs Fast Copy vs chatGptDeepClone
Created:
2 years ago
by:
Registered User
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 type="module"> import fastCopy from 'https://cdn.skypack.dev/fast-copy'; window.fastCopy = fastCopy; </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; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; }
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Native structuredClone
myCopy = structuredClone(MyObject);
recursiveDeepCopy
myCopy = recursiveDeepCopy(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Fast Copy
myCopy = fastCopy(MyObject);
chatGptDeepClone
function chatGptDeepClone(obj) { if (obj === null || typeof obj !== 'object') { return obj; } let clone = Array.isArray(obj) ? [] : {}; for (let key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { clone[key] = chatGptDeepClone(obj[key]); } } return clone; } myCopy = chatGptDeepClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Native structuredClone
recursiveDeepCopy
Json Clone
Fast Copy
chatGptDeepClone
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):
This is a complex benchmark test that compares the performance of different methods for creating deep copies of objects in JavaScript. **Methods being compared:** 1. **Lodash cloneDeep**: A popular utility library that provides a `cloneDeep` function for creating deep copies of objects. 2. **Native structuredClone**: The `structuredClone` function is a new method introduced in ECMAScript 2020, which allows creating deep copies of objects while preserving their structure and semantics. 3. **recursiveDeepCopy**: A custom implementation of deep copying using recursion. 4. **JSON clone**: Using the `JSON.stringify` and `JSON.parse` methods to create a deep copy of an object by serializing it to a string and then parsing it back into an object. 5. **Fast Copy**: A third-party library (not the official FastCopy) that provides a high-performance way to create deep copies of objects. **Pros and Cons:** * **Lodash cloneDeep**: Pros: widely used, well-tested, and reliable. Cons: adds external dependency, which may not be desirable for all use cases. * **Native structuredClone**: Pros: built-in, efficient, and preserves object structure. Cons: requires modern JavaScript engine support (ECMAScript 2020+). * **recursiveDeepCopy**: Pros: custom implementation allows fine-tuning, but requires more code maintenance and potential performance issues due to recursion overhead. * **JSON clone**: Pros: lightweight, no external dependencies required. Cons: may lose some object properties or semantics during serialization. * **Fast Copy**: Pros: optimized for high-performance copying, minimal external dependencies. Cons: not officially maintained or documented, potentially less reliable. **Library/Function explanations:** * **Lodash**: A popular utility library that provides a wide range of functions for various tasks, including deep copying. * **structuredClone**: The `structuredClone` function creates a new object with the same structure and semantics as the original, preserving all its properties and relationships. * **Fast Copy**: While not explicitly mentioned in the provided code, FastCopy is a third-party library that uses a custom implementation to create high-performance deep copies of objects. **Special JS feature/syntax:** The benchmark test makes use of ECMAScript 2020 features, specifically the `structuredClone` function. This function was introduced as part of the ECMAScript 2020 standard and allows creating deep copies of objects while preserving their structure and semantics. **Alternatives:** Other alternatives for deep copying in JavaScript include: * **Object.assign()**: While not suitable for all use cases (e.g., when dealing with circular references), `Object.assign()` can be used to create shallow copies of objects. * **for...in loops**: This approach involves manually iterating over an object's properties and assigning them to new objects. However, it can become cumbersome for complex objects and may lead to performance issues. Keep in mind that the choice of deep copying method depends on specific use cases, performance requirements, and personal preferences.
Related benchmarks:
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (small object): Lodash <> Custom clone func <> JSON.parse <> structuredClone 2
Comparing deep cloning methods (large array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comparing deep cloning methods (Complex object): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?