Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Most Performant JS DeepCopy
// from https://jsperf.com/deep-copy-vs-json-stringify-json-parse/5
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment