Skip to content

Instantly share code, notes, and snippets.

@mix3d
Created August 8, 2018 21:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mix3d/381e7797360bf5b4cc062885d864aa4f to your computer and use it in GitHub Desktop.
Save mix3d/381e7797360bf5b4cc062885d864aa4f to your computer and use it in GitHub Desktop.
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