Skip to content

Instantly share code, notes, and snippets.

@izy521
Created July 30, 2016 17:28
Show Gist options
  • Save izy521/4d394dec28054d54684269d91b16cb8a to your computer and use it in GitHub Desktop.
Save izy521/4d394dec28054d54684269d91b16cb8a 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.
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;
}
}
@izy521
Copy link
Author

izy521 commented Jul 30, 2016

JSON: 821ms
DeepCopy: 323ms
DeepCopyMap: 286ms
PrototypeExtensionCopy: 266ms

These are pretty much the times I was expecting.
Figured JSON would be the slowest because of anything extra the spec defines in the back-end.
Figured DeepCopy would be second slowest because of the switch-case.
Figured DeepCopyMap would be second fastest because of the ability to use anything as a Map's key.
Figured PrototypeExtensionCopy would be the fastest because as of now, accessing JavaScript objects is faster than an ES6 Map. But since they use Strings (and Symobls) it would be the slowest if I were using Object.prototype.toString.call(var) as the keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment