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 [discussion on StackOverflow](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. *Edit:* * added more value types to clone * added internal and self references to the object to clone. * added tests for major libs providing a clone() function 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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script src="http://documentcloud.github.com/underscore/underscore-min.js"> </script> <script src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"> </script> <script src="http://yui.yahooapis.com/combo?3.5.0/build/yui-base/yui-base-min.js&3.5.0/build/oop/oop-min.js"> </script> <script src="https://ajax.googleapis.com/ajax/libs/dojo/1.7.2/dojo/dojo.js"> </script> <script src="http://cdn.sencha.io/ext-4.0.7-gpl/ext-all.js"> </script> <script src="http://oranlooney.com/static/javascript/deepCopy.js"></script> <script> var oldObject = { a: 1, b: "str", c: true, d: false, e: null, f: function() { return 6; }, g: [7, 8, 9], h: new Date(), i: { a: new Date() }, f: /asd/i, u: undefined }; // references oldObject.ref = oldObject.g; oldObject.i.deep_ref = oldObject.f; // cyclic reference //oldObject.self = oldObject; function clone(obj) { var target = {}; for (var i in obj) { if (obj.hasOwnProperty(i)) { target[i] = clone(obj[i]); } } return target; } function clone2(o, b) { var a; if (Date.isInstanceOfType(o)) { return new Date((o).getTime()); } else if (Array.isInstanceOfType(o)) { a = []; } else if (Function.isInstanceOfType(o)) { return o; } else if (Object.isInstanceOfType(o)) { a = {}; } else { return o; } for (var i in o) { if (b || o.hasOwnProperty(i)) { a[i] = clone2(o[i]); } } return a; } function cloneJsonObject(o) { // similar to clone2, but assuming o came from a JSON string, so we can skip hasOwnProperty checks var a; if (Date.isInstanceOfType(o)) { return new Date((o).getTime()); } else if (Array.isInstanceOfType(o)) { a = []; } else if (Function.isInstanceOfType(o)) { return o; } else if (Object.isInstanceOfType(o)) { a = {}; } else { return o; } for (var i in o) { a[i] = cloneJsonObject(o[i]); } return a; } 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 } }); //setup YUI module Y = YUI().use('oop'); </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 = clone(oldObject); Test 4 Title Async (check if this is an asynchronous test) Code var newObject = Object.clone(oldObject); Test 5 Title Async (check if this is an asynchronous test) Code // This properly clones dates, but it is slow var newObject = Sys.Serialization.JavaScriptSerializer.deserialize(Sys.Serialization.JavaScriptSerializer.serialize(oldObject)); Test 6 Title Async (check if this is an asynchronous test) Code // This properly clones dates and is faster than serializing var newObject = clone2(oldObject, true); Test 7 Title Async (check if this is an asynchronous test) Code // Similar to the above, but assumes all properties in the object must be cloned, therefore skips the hasOwnProperty check. var newObject = cloneJsonObject(oldObject); Test 8 Title Async (check if this is an asynchronous test) Code var newObject = _.clone(oldObject) Test 9 Title Async (check if this is an asynchronous test) Code var newObject = Y.clone(oldObject, true) // safe: no prototype cloning Test 10 Title Async (check if this is an asynchronous test) Code var newObject = dojo.clone(oldObject) Test 11 Title Async (check if this is an asynchronous test) Code var newObject = Ext.clone(oldObject) Test 12 Title Async (check if this is an asynchronous test) Code var newObject = owl.deepCopy(oldObject);