Editing Cloning an Object This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) 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. (see Revision 3 by Corban Brook) Revision 22: added LoDash since someone brought Underscore, using CDN for UnderScore Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="http://code.jquery.com/jquery-1.5.1.js" type="text/javascript"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script> <script>var underscore = _.noConflict();</script> <script src="https://raw.github.com/bestiejs/lodash/master/lodash.min.js"></script> <script>var lodash = _.noConflict();</script> <script> //allow UnderScore vs LoDash var lodash = window.lodash, underscore = window.underscore; //continue test setup var oldObject = { a: 1, b: 2, c: 3, d: 4, e: 5, f: function() { return 6; }, g: [7, 8, 9] }; function clone(obj) { var target = {}; for (var i in obj) { if (obj.hasOwnProperty(i)) { target[i] = obj[i]; } } return target; } function clone_deep(obj) { var target = {}; for (var i in obj) { if (typeof(obj) === 'object') { target[i] = clone_deep(obj[i]); } else { target[i] = obj[i]; } } return target; } Object.defineProperties(Object, { 'extend': { 'configurable': true, 'enumerable': false, 'value': function extend(what, wit) { var extObj, witKeys = Object.keys(wit); extObj = Object.keys(what).length ? Object.clone(what) : {}; witKeys.forEach(function(key) { Object.defineProperty(extObj, key, Object.getOwnPropertyDescriptor(wit, key)); }); return extObj; }, 'writable': true }, 'clone': { 'configurable': true, 'enumerable': false, 'value': function clone(obj) { return Object.extend({}, obj); }, 'writable': true } }); function struct_clone (oToBeCloned) { if (oToBeCloned === null || !(oToBeCloned instanceof Object)) { return oToBeCloned; } var oClone, fConstr = oToBeCloned.constructor; switch (fConstr) { // implement other special objects here! case RegExp: /* oClone = new fConstr(oToBeCloned.source, Array.prototype.filter.call("gim", function () { return (oToBeCloned.global | oToBeCloned.ignoreCase << 1 | oToBeCloned.multiline) & 1 << arguments[1]; }).join("")); */ oClone = new fConstr(oToBeCloned.source, "g".substr(0, Number(oToBeCloned.global)) + "i".substr(0, Number(oToBeCloned.ignoreCase)) + "m".substr(0, Number(oToBeCloned.multiline))); break; case Date: oClone = new fConstr(oToBeCloned.getTime()); break; // etc. default: oClone = new fConstr(); } for (var sProp in oToBeCloned) { oClone[sProp] = clone(oToBeCloned[sProp]); } return oClone; } </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var newObject = jQuery.extend(true, {}, oldObject); Test 2 Title Async (check if this is an asynchronous test) Code var newObject = JSON.parse(JSON.stringify(oldObject)); Test 3 Title Async (check if this is an asynchronous test) Code var newObject = jQuery.extend({}, oldObject); Test 4 Title Async (check if this is an asynchronous test) Code var newObject = clone(oldObject); Test 5 Title Async (check if this is an asynchronous test) Code var newObject = Object.clone(oldObject); Test 6 Title Async (check if this is an asynchronous test) Code var newObject = oldObject.slice(0); Test 7 Title Async (check if this is an asynchronous test) Code var newObject = underscore.clone(oldObject); Test 8 Title Async (check if this is an asynchronous test) Code var newObject = lodash.clone(oldObject); Test 9 Title Async (check if this is an asynchronous test) Code var newObject = clone_deep(oldObject); Test 10 Title Async (check if this is an asynchronous test) Code var newObject = struct_clone(oldObject);