Created
August 8, 2018 21:18
-
-
Save mix3d/381e7797360bf5b4cc062885d864aa4f to your computer and use it in GitHub Desktop.
Most Performant JS DeepCopy
This file contains hidden or 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