deepClone Test

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code

<script src="http://code.jquery.com/jquery-latest.pack.js">
</script>
      
<script>
Benchmark.prototype.setup = function() {
  Object.prototype.clone = function(deep, falseArray /*check*/ ) {
    var type = Object.prototype.toString.call(this).match(/^\[object (.+?)\]$/)[1];
  /*var falseArray;
  	if(check) {
  	falseArray = (Object.keys(this).length - this.length);
  	}*/
    //takes around 400ms additional at 500k array to check if it has additional properties, just pass it as argument decide if oy
    switch (type) {
    case "Array":
      var clone = [];
      if (!falseArray) {
        if (!deep) clone = this.concat();
        else this.forEach(function(e) {
          if (typeof e !== "undefined" && e !== null) clone.push((typeof e !== "object" ? e : e.clone((typeof deep == "boolean" ? deep : (deep - 1)))));
          else clone.push("");
        });
      } else { // Variable is an 'Array' but has an extra propertie e.g: var arr = [1,2,3]; arr.a = "b" //its the slowest possibility but normally Objects would be used
        for (var prop in this) {
          clone[prop] = this[prop];
        }
      }
      break;
    case "Object":
      var clone = {};
      if (!deep) {
        for (var prop in this) {
          clone[prop] = this[prop];
        }
      } else {
        for (var prop in this) {
          if (typeof this[prop] !== "undefined" && this[prop] !== null) clone[prop] = (typeof this[prop] !== "object" ? this[prop] : this[prop].clone((typeof deep == "boolean" ? deep : (deep - 1))));
          else clone[prop] = "";
        }
      }
      break;
    default:
      var clone = this.valueOf();
      break;
    }
    return clone;
  };
  Object.defineProperty(Object.prototype, "clone", {
    enumerable: false
  });
  
  var testArray = [];
  for (var i = 0; i < 5000000; i++) {
    testArray.push(i);
  }
  var testObject = {};
  for (var i = 0; i < 500000; i++) {
    testObject[i] = i;
  }
  var testArrayClone = [];
  var testArrayJqueryEachCopy = [];
  
  var testObject = {};
  for (var i = 0; i < 500000; i++) {
    testObject[i] = i;
  }
  var testObjectJqueryEachCopy = {};
  var testObjectClone = {};
  
  var testArray2 = [];
  for (var i = 0; i < 500000; i++) {
    testArray2.push([i, "a", "b"]);
  }
  var testArrayJqueryEachCopy2 = [];
  var testArrayClone2 = [];
  
  var testObject2 = {};
  for (var i = 0; i < 500000; i++) {
    testObject2[i] = {
      i: i,
      a: "b",
      b: "c"
    };
  }
  var testObjectJqueryEachCopy2 = {};
  var testObjectClone2 = {};

};
</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
Code Snippet
testArrayClone = testArray.clone(0);
testObjectClone = testObject.clone(0);
testArrayClone2 = testArray2.clone(1);
testObjectClone2 = testObject2.clone(1);
pending…
jQuery.each
$.each(testArray, function(i, e) {
  testArrayJqueryEachCopy.push(e);
});
$.each(testObject, function(i, e) {
  testObjectJqueryEachCopy[i] = e;
});
$.each(testArray2, function(i, e) {
  var z = [];
  $.each(e, function(j, f) {
    z.push(f);
  });
  testArrayJqueryEachCopy2.push(z);
});
$.each(testObject2, function(i, e) {
  var z = {};
  $.each(e, function(j, f) {
    z[j] = f;
  });
  testObjectJqueryEachCopy2[i] = z;
});
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