shallow clone hand written vs generic

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var objToClone = {
      prop1: '1',
      prop2: '2',
      prop3: '3',
      prop4: '4',
      prop5: '5',
      prop6: '6',
      prop7: '7'
    };
   
   
    function handShallowClone(obj) {
      return {
        prop1: obj.prop1,
        prop2: obj.prop2,
        prop3: obj.prop3,
        prop4: obj.prop4,
        prop5: obj.prop5,
        prop6: obj.prop6,
        prop7: obj.prop7
      };
    }
   
    var evaledClone = eval('(' + handShallowClone.toString() + ')');
   
    function shallowClone(obj) {
      var newObj = {};
      for (var i in obj) {
        if (obj.hasOwnProperty(i)) {
          newObj[i] = obj[i];
        }
      }
      return newObj;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
by hand written specialized
handShallowClone(objToClone);
pending…
generic
shallowClone(objToClone);
pending…
evaled specialized
evaledClone(objToClone);
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:

0 comments

Add a comment