Cloning an Object
JavaScript performance comparison
Info
There is no quick and easy facility for cloning an object, Some people recommend using JQuery.extend others JSON.parse/stringify
http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object
If you want the fastest possible clone function. I would personally anticipate the data structure of your object and write a custom clone to handle it.
Preparation code
<script src="http://code.jquery.com/jquery-1.5.1.js" type="text/javascript">
</script>
<script>
var oldObject = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
g: {
a: [7, 8, 9, 54.6435, 354, 353, 453.4, 534, 53.4, 5.34, 52.4, 234, 6, 678, 76.84, 53, 73, 54.2, 57, 45, 3, 67, 78, 7265, 7, 546, 37, 8, 6, 8, 436.4, 64.3, 6, 8, 7, 6, 74.7, 3634.636, 43.6, 567.8735, 3675, 7346, 834, 737643, 6, 7436, 57, 47, 57.46, 57, 46, 74.6, 7, 56.7, 26.3, 67.7, 5474.5745, 754, 754, 745, 7],
b: [354, 35, 1, 312, 32.4, 35, 7, 76878.8, 686.7, 78.98, 77, 46.3, 3452, 13, 435, 4, 86.7, 987.89, 86775.6, 4565, 88.9, 9, 6, 75678, 9, 9067, 46, 356.65, 8.95, 246.5, 87, 678, 465.6, 86.8, 63, 56, 867.86, 43, 65.67, 586.85, 734, 77, 867, 43, 6, 67, 67.864757, 86, 8, 3.62, 5, 6, 767, 9878.85, 4.6, 867, 7436, 7, 4, 721, 5, 765, 8, 6, 3245, 7548, 53.67, 437.6],
c: [7, 8, 9, 54.6435, 354, 353, 453.4, 534, 53.4, 5.34, 52.4, 234, 6, 678, 76.84, 53, 73, 54.2, 57, 45, 3, 67, 78, 726.5, 7, 54.6, 37, 8, 6, 8, 436.4, 64.3, 6, 8, 7, 6, 74.7, 3634.636, 43.6, 567.86735, 3675, 734.6, 834, 737.643, 6, 74.36, 57, 47, 57.46, 57, 46, 74.6, 7, 56.7, 26.3, 67.7, 5474.5745, 754, 754, 74.5, 7]
}
};
var object_create = Object.create;
if (typeof object_create !== 'function') {
object_create = function(o) {
function F() {}
F.prototype = o;
return new F();
};
}
function deepCopy(src) {
if (src === null || typeof(src) !== 'object') {
return src;
}
//Honor native/custom clone methods
if (typeof src.clone === 'function') {
return src.clone(true);
}
//Special cases:
//Date
if (src instanceof Date) {
return new Date(src.getTime());
}
//RegExp
if (src instanceof RegExp) {
return new RegExp(src);
}
//DOM Elements
if (src.nodeType && typeof src.cloneNode === 'function') {
return src.cloneNode(deep);
}
var proto = (Object.getPrototypeOf ? Object.getPrototypeOf(src) : src.__proto__);
if (!proto) {
proto = src.constructor.prototype;
}
var ret = object_create(proto);
for (var key in src) {
ret[key] = deepCopy(src[key]);
}
return ret;
}
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
JQuery.extend deep |
|
pending… |
JSON |
|
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. Here’s a list of current revisions for this page:
- Revision 1: published by Corban Brook
- Revision 2: published by Corban Brook
- Revision 3: published by Corban Brook
- Revision 4: published by siR
- Revision 6: published and last updated
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published by Matteo Mirk
- Revision 14: published by Derek
- Revision 15: published and last updated
- Revision 16: published
- Revision 17: published by Felix
- Revision 18: published
- Revision 19: published and last updated
- Revision 20: published
- Revision 21: published by @murger
- Revision 22: published by tomByrer
- Revision 23: published by Jesper Petersson
- Revision 24: published by check if all arrays are empty
- Revision 25: published
- Revision 26: published
- Revision 27: published by collibra
- Revision 28: published by A. Matías Quezada
- Revision 32: published by Abdullah
- Revision 34: published
- Revision 36: published
- Revision 40: published
- Revision 41: published
- Revision 42: published
- Revision 43: published by Jonathan Perry
- Revision 44: published by Jonathan Perry
- Revision 45: published
- Revision 46: published by Alex Rodin
- Revision 48: published by cezary
- Revision 49: published
- Revision 50: published
- Revision 52: published
- Revision 53: published
- Revision 54: published
- Revision 55: published
- Revision 56: published
- Revision 57: published
- Revision 58: published
- Revision 60: published by Stefano Sergio
- Revision 61: published by cfstras
- Revision 62: published
- Revision 63: published
- Revision 65: published by Jeremy Banks
- Revision 66: published
0 comments