-
-
Save minwe/fe0ed2619a6f31158bf67d09cb1cf4a4 to your computer and use it in GitHub Desktop.
`JSON.parse( JSON.stringify( obj) )` has been regarded as the fastest method for deep copying Objects, but is it? This is mainly just to test. Obviously Functions aren't allowed in JSON.
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
var Types = new Map(); | |
Types.set(Array, function(v) { | |
var l = v.length; i = 0, a = Array(l); | |
for (i; i<l; i++) { | |
a[i] = v[i]; | |
} | |
return a; | |
}); | |
Types.set(Number, function(v) { | |
return v * 1; | |
}); | |
Types.set(String, function(v) { | |
return String(v); | |
}); | |
Types.set(Function, function(v) { | |
return eval(v); | |
}); | |
Types.set(Boolean, function(v) { | |
return !!v; | |
}); | |
//Extending Prototypes (Obviously not for production) | |
//No nerds allowed. | |
//I'm dangerous. | |
//Can't think of a name for the method. | |
Array.prototype.izyCopy = function(v) { | |
var l = v.length; i = 0, a = Array(l); | |
for (i; i<l; i++) { | |
a[i] = v[i]; | |
} | |
return a; | |
}; | |
Number.prototype.izyCopy = function(v) { | |
return v * 1; | |
}; | |
String.prototype.izyCopy = function(v) { | |
return String(v); | |
}; | |
Function.prototype.izyCopy = function(v) { | |
return eval(v); | |
}; | |
Boolean.prototype.izyCopy = function(v) { | |
return !!v; | |
}; | |
var obj = { | |
first: { | |
second: { | |
val: true, | |
num: 32, | |
s: "Hello World", | |
third: { | |
justbecause: null | |
}, | |
}, | |
thing: false, | |
tf: function() {} | |
}, | |
k: true, | |
v: false | |
}; | |
var limit = 100000; | |
var i = 0; | |
var o = 0; | |
var z = 0; | |
var n = 0; | |
console.time("JSON"); | |
for (i; i<limit; i++) { | |
JSON.parse(JSON.stringify(obj)); | |
} | |
console.timeEnd("JSON"); | |
console.time("DeepCopy"); | |
for (o; o<limit; o++) { | |
copyObj(obj); | |
} | |
console.timeEnd("DeepCopy"); | |
console.time("DeepCopyMap"); | |
for (z; z<limit; z++) { | |
copyObjMap(obj); | |
} | |
console.timeEnd("DeepCopyMap"); | |
console.time("PrototypeExtensionCopy"); | |
for (n; n<limit; n++) { | |
copyObjProto(obj); | |
} | |
console.timeEnd("PrototypeExtensionCopy"); | |
function copyObjProto(o) { | |
var k = Object.keys(o), io = {}, i = k.length; | |
for (i; i--;) { | |
if ( o[k[i]] && o[k[i]].constructor === Object ) { | |
io[ k[i] ] = copyObjProto(o[k[i]]); | |
continue; | |
} | |
if (o[k[i]] === null) { | |
io[ k[i] ] = null; | |
continue; | |
} | |
io[ k[i] ] = o[k[i]].izyCopy(o[k[i]]); | |
} | |
return io; | |
} | |
function copyObjMap(o) { | |
var k = Object.keys(o), io = {}, i = k.length; | |
for (i; i--;) { | |
if ( o[k[i]] && o[k[i]].constructor === Object ) { | |
io[ k[i] ] = copyObjMap(o[k[i]]); | |
continue; | |
} | |
io[ k[i] ] = copyMap(o[k[i]]); | |
} | |
return io; | |
} | |
function copyMap(v) { | |
if (v === null) return null; | |
return Types.get(v.constructor)(v); | |
} | |
function copyObj(o) { | |
var k = Object.keys(o), io = {}, i = k.length; | |
for (i; i--;) { | |
if ( o[k[i]] && o[k[i]].constructor === Object ) { | |
io[ k[i] ] = copyObj(o[k[i]]); | |
continue; | |
} | |
io[ k[i] ] = copy(o[k[i]]); | |
} | |
return io; | |
} | |
function copy(v) { | |
if (v === null) return null; | |
switch(v.constructor) { | |
case Array: | |
var l = v.length; i = 0, a = Array(l); | |
for (i; i<l; i++) { | |
a[i] = v[i]; | |
} | |
return a; | |
case Number: | |
return v * 1; | |
case String: | |
return String(v); | |
case Function: | |
return eval(v); | |
case Boolean: | |
return !!v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment