<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>
function recursiveDeepCopy(obj) {
return Object.keys(obj).reduce((v, d) => Object.assign(v, {
[d]: (obj[d].constructor === Object) ? recursiveDeepCopy(obj[d]) : obj[d]
}), {});
}
function recursiveDeepCopy2(obj) {
return Object.keys(obj).reduce((v, d) => Object.assign(v, {
[d]: obj[d]
? Object.assign(v, {
[d]: obj[d].constructor === Object ? deepClone(obj[d]) : obj[d]
})
: v
}), {});
}
function deepClone(object) {
let result;
let i;
let l;
if (typeof object !== 'object') {
return object;
}
if (!object) {
return object;
}
if (object.constructor === Array) {
result = [];
l = object.length;
for (i = 0; i < l; i++) {
result[i] = deepClone(object[i]);
}
return result;
}
result = {};
for (i in object) {
result[i] = deepClone(object[i]);
}
return result;
}
function jsonDeepCopy(o) {
return JSON.parse(JSON.stringify(o));
}
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
}
}
}
}]
}]
var dimensionsCopy = recursiveDeepCopy(dimensions);
var dimensionsCopy = jsonDeepCopy(dimensions);
var dimensionsCopy = _.cloneDeep(dimensions);
var dimensionsCopy = deepClone(dimensions);
var dimensionsCopy = recursiveDeepCopy2(dimensions);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Recursive Deep Copy | |
JSON Deep Copy | |
lodash clone | |
deepclone | |
Recursive Deep Copy 2 |
Test name | Executions per second |
---|---|
Recursive Deep Copy | 1078151.9 Ops/sec |
JSON Deep Copy | 90982.4 Ops/sec |
lodash clone | 52656.4 Ops/sec |
deepclone | 83826.0 Ops/sec |
Recursive Deep Copy 2 | 80005.5 Ops/sec |
I'll provide the answer based on my understanding of the provided text.
It appears that we have three different methods for recursively copying objects or arrays:
recursiveDeepCopy(dimensions)
: This function is not defined in the provided text, but its name suggests it's a custom implementation for recursive deep copying.jsonDeepCopy(dimensions)
: This function is also not defined, but it seems to be another custom implementation for JSON-specific deep copying..cloneDeep(dimensions)
and deepClone(dimensions)
: These are built-in functions from the Lodash library (_.cloneDeep
) and a separate library called deepclone
(which is likely a wrapper around Lodash or another implementation).The benchmark results show that:
recursiveDeepCopy
) has the highest execution rate, followed by JSON Deep Copy.deepClone
function has an even lower execution rate than the Lodash clone function.These results suggest that the custom implementation (recursiveDeepCopy
) is likely the fastest, followed by the Lodash implementation (_.cloneDeep
). However, please note that this is just a conclusion based on limited data and may not be representative of all use cases or environments.