-
-
Save derrabauke/d79899cb790d0b945f713054ad67b2d1 to your computer and use it in GitHub Desktop.
Most Performant JS DeepCopy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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