cloning an object

JavaScript performance comparison

Revision 36 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  function Classy(init) {
    var internal = init || 0;
    return { "increment": function () { internal++; },
             "getInternal": function () { return internal },
             "clone": function () { return new Classy(internal); }
    }
  };
  var complexobj = {
      "a": "a string",
      "b": 123456789,
      "c": true,
      "d": null,
      "e": ["a",2,false,null,{}],
      "f": function () { return true; },
      "g": new Date(),
      "h": new RegExp(/aaa/i),
      "i": new Classy(),
      "j": {
          "1":  "a string",
          "2":  123456789,
          "3":  true,
          "4":  null,
          "5":  ["a",2,false,null,{}],
          "6":  function () { return true; },
          "7":  new Date(),
          "8":  new RegExp(/aaa/i),
          "9":  new Classy(),
          "10": {
              "i":    "a string",
              "ii":   123456789,
              "iii":  true,
              "iv":   null,
              "v":    ["a",2,false,null,{}],
              "vi":   function () { return true; },
              "vii":  new Date(),
              "viii": new RegExp(/aaa/i),
              "ix":   new Classy()
          }
      }
  };
  
  
  function clone(obj) {
      var copy;
      
      //types where returning the object will naturally result in a clone
      var objtype = typeof obj;
      if (objtype === "undefined" || obj === null
        || (objtype !== "object" && objtype !== "function")) {
          return obj;
      }
  
      if (objtype === "function") {
          if (obj.hasOwnProperty("clone")) {
              return obj.clone();
          } else {
              return eval("(" + obj.toString() + ")");
          }
      }
  
      if (obj instanceof Date) {
          copy = new Date();
          copy.setTime(obj.getTime());
          return copy;
      }
  
      if (obj instanceof RegExp) {
          copy = new RegExp(obj.toString());
          return copy;
      }
  
      if (obj instanceof Array) {
          copy = [];
          for (var i=0; i<obj.length; i++) {
              copy[i] = clone(obj[i]);
          }
          return copy;
      }
  
      if (obj instanceof Object) {
          copy = {};
          var keys = Object.keys(obj);
          if (obj.hasOwnProperty("clone")) {
              return obj.clone();
          } else if (keys.length > 0) {
              for (var i=0; i<keys.length; i++) {
                  var key = keys[i];
                  copy[key] = clone(obj[key]);
              }
              return copy;
          } else {
              //not sure all types of objects that would get you into this scenario, so this may take some tweaking
              return JSON.parse(JSON.stringify(obj));
          }
      }
  }
  
  function replaceStateClone(obj) {
      var oldState = history.state;
      history.replaceState(obj, null);
      var clonedObj = history.state;
      history.replaceState(oldState, null);
      return clonedObj;
  }

};
</script>

Preparation code output

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
JSON stringify/parse
var newObject = JSON.parse(JSON.stringify(complexobj));
pending…
jQuery.extend() deep
var newObject = jQuery.extend(true, {}, complexobj);
pending…
clone function
var newObject = clone(complexobj);
pending…
stringify eval
var newObject = eval("(" + JSON.stringify(complexobj) + ")");
pending…
_.extend
var newObject = _.cloneDeep(complexobj);
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.

0 Comments