Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Deep Copy (+customs)
(version: 0)
Produce a deep copy of a Javascript object where nested objects are not simply references to the originals.
Comparing performance of:
Recursive Deep Copy vs JSON Deep Copy vs lodash clone vs customDeepCloneAegis vs customDeepCloneAutobound
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
Script Preparation code:
function recursiveDeepCopy(obj) { return Object.keys(obj).reduce((v, d) => Object.assign(v, { [d]: (obj[d].constructor === Object) ? recursiveDeepCopy(obj[d]) : obj[d] }), {}); } function jsonDeepCopy(o) { return JSON.parse(JSON.stringify(o)); } function customDeepCloneAegis(obj) { if (obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj) { return obj; } let temp; if (obj instanceof Date) { temp = new obj.constructor(); } else { temp = obj.constructor(); } for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { obj.isActiveClone = null; temp[key] = customDeepCloneAegis(obj[key]); delete obj.isActiveClone; } } return temp; } function customDeepCloneAutobound(o) { let newO, i; if (typeof o !== 'object' || !o) return o; if (Object.prototype.toString.apply(o) === '[object Array]') { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = customDeepCloneAutobound(o[i]); } return newO; } newO = {} for (i in o) { // eslint-disable-next-line if (o.hasOwnProperty(i)) { newO[i] = customDeepCloneAutobound(o[i]); } } return newO; } var dimensions = [{ "dimensions": [{ "runtime": { "common": { "client": null, "server": null } } }, { "device": { "android": null, "blackberry": null, "iemobile": null, "iphone": null, "ipad": null, "kindle": null, "opera-mini": null, "palm": null } }, { "environment": { "development": { "dev": null, "test": null }, "production": { "stage": null, "prod": null } } }, { "lang": { "ar": { "ar-JO": null, "ar-MA": null, "ar-SA": null, "ar-EG": null }, "bn": { "bn-IN": null }, "ca": { "ca-ES": null }, "cs": { "cs-CZ": null }, "da": { "da-DK": null }, "de": { "de-AT": null, "de-DE": null }, "el": { "el-GR": null }, "en": { "en-AU": null, "en-BG": null, "en-CA": null, "en-GB": null, "en-GY": null, "en-HK": null, "en-IE": null, "en-IN": null, "en-MY": null, "en-NZ": null, "en-PH": null, "en-SG": null, "en-US": null, "en-ZA": null }, "es": { "es-AR": null, "es-BO": null, "es-CL": null, "es-CO": null, "es-EC": null, "es-ES": null, "es-MX": null, "es-PE": null, "es-PY": null, "es-US": null, "es-UY": null, "es-VE": null }, "fi": { "fi-FI": null }, "fr": { "fr-BE": null, "fr-CA": null, "fr-FR": null, "fr-GF": null }, "hi": { "hi-IN": null }, "hu": { "hu-HU": null }, "id": { "id-ID": null }, "it": { "it-IT": null }, "ja": { "ja-JP": null }, "kn": { "kn-IN": null }, "ko": { "ko-KR": null }, "ml": { "ml-IN": null }, "mr": { "mr-IN": null }, "ms": { "ms-MY": null }, "nb": { "nb-NO": null }, "nl": { "nl-BE": null, "nl-NL": null, "nl-SR": null }, "pl": { "pl-PL": null }, "pt": { "pt-BR": null }, "ro": { "ro-RO": null }, "ru": { "ru-RU": null }, "sv": { "sv-SE": null }, "ta": { "ta-IN": null }, "te": { "te-IN": null }, "th": { "th-TH": null }, "tr": { "tr-TR": null }, "vi": { "vi-VN": null }, "zh": { "zh-Hans": { "zh-Hans-CN": null }, "zh-Hant": { "zh-Hant-HK": null, "zh-Hant-TW": null } } } }] }]
Tests:
Recursive Deep Copy
var dimensionsCopy = recursiveDeepCopy(dimensions);
JSON Deep Copy
var dimensionsCopy = jsonDeepCopy(dimensions);
lodash clone
var dimensionsCopy = _.cloneDeep(dimensions);
customDeepCloneAegis
var dimensionsCopy = customDeepCloneAegis(dimensions);
customDeepCloneAutobound
var dimensionsCopy = customDeepCloneAutobound(dimensions);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Recursive Deep Copy
JSON Deep Copy
lodash clone
customDeepCloneAegis
customDeepCloneAutobound
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):
It seems like you're preparing to compare the performance of four different functions for deep copying JavaScript objects: `recursiveDeepCopy`, `jsonDeepCopy`, `_.cloneDeep` from Lodash, and two custom implementations, `customDeepCloneAegis` and `customDeepCloneAutobound`. Based on the provided benchmark results, here's a summary: * **Recursive Deep Copy** (`recursiveDeepCopy`) is relatively fast with an average of 321,778 executions per second. * **JSON Deep Copy** (`jsonDeepCopy`) has a lower execution rate with an average of 36,390 executions per second. * The Lodash function `_.cloneDeep` outperforms the JSON deep copy method with an average of 30,244 executions per second. * The custom implementations are less efficient: * **customDeepCloneAegis** has a significantly slower execution rate with an average of 22,163 executions per second. * **customDeepCloneAutobound** performs even worse with an average of 13,192 executions per second. These results suggest that the Lodash function `_.cloneDeep` is likely to be the most efficient choice for deep copying JavaScript objects, followed closely by the recursive deep copy method. The custom implementations are less suitable for performance-critical applications. To further improve the custom implementations, consider investigating potential optimizations such as: * Using more efficient data structures or algorithms. * Minimizing object creation and copying overhead. * Utilizing browser-specific features or built-in methods that might provide better performance.
Related benchmarks:
Object Deep Copy
Object Deep Copyit
Object Deep Copy 2
Object Deep Copy (+customs)(+customs)
Comments
Confirm delete:
Do you really want to delete benchmark?