Deep Copy vs JSON Stringify / JSON Parse
JavaScript performance comparison
Preparation code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
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;
}
function deepCopy(o) {
var copy = o,
k;
if (o && typeof o === 'object') {
copy = Object.prototype.toString.call(o) === '[object Array]' ? [] : {};
for (k in o) {
copy[k] = deepCopy(o[k]);
}
}
return copy;
}
function clone(obj) {
if (obj == null || typeof(obj) != 'object')
return obj;
var temp = new obj.constructor();
for (var key in obj)
temp[key] = clone(obj[key]);
return temp;
}
var uc = {
"list": {
"0oVwOM": {
"id": "0oVwOM",
"parent": "pTlmbh",
"name": "New node",
"created_at": 1384289621
},
"aHxe8k": {
"id": "aHxe8k",
"parent": "Fhs2hL",
"name": "hjkhjkhjk",
"created_at": 1384354593
},
"Fhs2hL": {
"id": "Fhs2hL",
"parent": "root",
"name": "test",
"created_at": 1383403881
},
"HYPSgv": {
"id": "HYPSgv",
"parent": "0oVwOM",
"name": "New node",
"created_at": 1384342657
},
"osFIMf": {
"id": "osFIMf",
"parent": "root",
"name": "New node",
"created_at": 1384354584
},
"PsovXE": {
"id": "PsovXE",
"parent": "root",
"name": "hjkhjkhjk",
"created_at": 1384354589
},
"pTlmbh": {
"id": "pTlmbh",
"parent": "Fhs2hL",
"name": "New node",
"created_at": 1384289277
},
"RbXhdJ": {
"id": "RbXhdJ",
"parent": "root",
"name": "empty",
"created_at": 1384359806
}
},
"maps": {
"parent": {
"pTlmbh": {
"0oVwOM": {
"id": "0oVwOM",
"parent": "pTlmbh",
"name": "New node",
"created_at": 1384289621
}
},
"Fhs2hL": {
"aHxe8k": {
"id": "aHxe8k",
"parent": "Fhs2hL",
"name": "hjkhjkhjk",
"created_at": 1384354593
},
"pTlmbh": {
"id": "pTlmbh",
"parent": "Fhs2hL",
"name": "New node",
"created_at": 1384289277
}
},
"root": {
"Fhs2hL": {
"id": "Fhs2hL",
"parent": "root",
"name": "test",
"created_at": 1383403881
},
"osFIMf": {
"id": "osFIMf",
"parent": "root",
"name": "New node",
"created_at": 1384354584
},
"PsovXE": {
"id": "PsovXE",
"parent": "root",
"name": "hjkhjkhjk",
"created_at": 1384354589
},
"RbXhdJ": {
"id": "RbXhdJ",
"parent": "root",
"name": "empty",
"created_at": 1384359806
}
},
"0oVwOM": {
"HYPSgv": {
"id": "HYPSgv",
"parent": "0oVwOM",
"name": "New node",
"created_at": 1384342657
}
}
},
"path": [
["Fhs2hL"],
["Fhs2hL", "aHxe8k"],
["Fhs2hL", "pTlmbh"],
["Fhs2hL", "pTlmbh", "0oVwOM"],
["Fhs2hL", "pTlmbh", "0oVwOM", "HYPSgv"],
["osFIMf"],
["PsovXE"],
["RbXhdJ"]
]
}
};
function recursive(obj) {
var clone, i;
if (typeof obj !== 'object' || !obj)
return obj;
if ('[object Array]' === Object.prototype.toString.apply(obj)) {
clone = [];
var len = obj.length;
for (i = 0; i < len; i++)
clone[i] = recursive(obj[i]);
return clone;
}
clone = {};
for (i in obj)
if (obj.hasOwnProperty(i))
clone[i] = recursive(obj[i]);
return clone;
}
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
Destructuring
|
|
pending… |
Deep Copy
|
|
pending… |
recursive
|
|
pending… |
JSON Stringify / JSON Parse
|
|
pending… |
$
|
|
pending… |
clone
|
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
- Revision 1: published ramesaliyev
- Revision 2: published
- Revision 3: published
- Revision 4: published
- Revision 5: published
- Revision 6: published
- Revision 7: published fafsdfssad
- Revision 8: published
- Revision 9: published dameleon
- Revision 10: published
- Revision 12: published Triplex Ultra lose fat
- Revision 15: published Test Thing and last updated
- Revision 21: published
- Revision 23: published
- Revision 24: published
- Revision 25: published
- Revision 26: published
- Revision 27: published
- Revision 28: published
- Revision 30: published
- Revision 33: published
- Revision 34: published evil twist
- Revision 35: published
- Revision 36: published
- Revision 37: published
- Revision 38: published
- Revision 39: published
- Revision 44: published
- Revision 45: published
- Revision 48: published
- Revision 53: published Jim Lake
- Revision 54: published Lune
- Revision 55: published
- Revision 58: published Dustin Falgout
- Revision 66: published Adrian
- Revision 67: published sample
- Revision 68: published Richard Davison
- Revision 69: published Richard Davison
- Revision 70: published Richard Davison
- Revision 71: published Richard Davison
- Revision 72: published Richard Davison
- Revision 73: published Richard Davison
- Revision 74: published Richard Davison
- Revision 75: published Richard Davison
- Revision 76: published Richard Davison
- Revision 77: published Richard Davison
- Revision 78: published Richard Davison
- Revision 79: published Richard Davison
- Revision 80: published
- Revision 81: published Luke Policinski
- Revision 82: published Ajay Kaushik
- Revision 83: published Ajay Kaushik
- Revision 84: published Ajay Kaushik
- Revision 85: published Marks Polakovs
- Revision 86: published Jonathan Perry
- Revision 87: published Jonathan Perry
- Revision 88: published Jonathan Perry
- Revision 89: published Jonathan Perry
- Revision 90: published Jonathan Perry
- Revision 91: published Jonathan Perry
- Revision 92: published Jonathan Perry
- Revision 93: published Jonathan Perry
- Revision 94: published Jonathan Perry
- Revision 95: published Jonathan Perry
- Revision 96: published Jonathan Perry
- Revision 97: published Jonathan Perry
- Revision 98: published Jonathan Perry
- Revision 99: published Jonathan Perry
- Revision 100: published Jonathan Perry
- Revision 101: published Jonathan Perry
- Revision 102: published Jonathan Perry
- Revision 103: published cainrus
- Revision 104: published Nicholas Mitchell
- Revision 105: published Nicholas Mitchell
- Revision 106: published Nicholas Mitchell
- Revision 107: published Nicholas Mitchell
- Revision 111: published Nicholas Mitchell
- Revision 112: published Magluf
- Revision 113: published Magluf
- Revision 114: published Magluf
- Revision 115: published Magluf
0 Comments