Extend Implementations

JavaScript performance comparison

Revision 2 of this test case created by Pencroff

Info

A comparision of different extend( obj1, obj2 ) implementations.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj1 = {
     name: 1,
     type: 2,
     size: 3,
     age: 4
    };
   
    var obj2 = {
     myName: 1,
     myType: 2,
     mySize: 3,
     myAge: 4
    };
   
    for (var k = 0; k < 25; k+=1) {
    obj1[k] = Math.random().toString(36);
    obj2['my' + k] = Math.random().toString(36);
    }
   
    function extend_for (target, source) {
   
        for (var k in source) {
          if (source.hasOwnProperty(k)) {
            target[k] = source[k];
          }
        }
   
        return target;
    }
   
   
    function extend_keys (target, source) {
   
        Object.keys(source).forEach(function(prop) {
          target[prop] = source[prop];
        });
   
        return target;
    }
   
    function extend_keys_while (target, source) {
      var keys = Object.keys(source),
          len = keys.length,
          i = 0,
          prop;
      while (i < len) {
        prop = keys[i];
        target[prop] = source[prop];
        i += 1;
      }
      return target;
    }
   
    function extend_names (target, source) {
        Object.getOwnPropertyNames(source).forEach(function(propName) {
   
          Object.defineProperty(target, propName, Object.getOwnPropertyDescriptor(source, propName));
        });
   
        return target;
    }
};

Benchmark.prototype.teardown = function() {
    var obj1 = {};
    var obj2 = {};
   
};
</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
extend_for
extend_for( obj1, obj2 );
pending…
extend_keys
extend_keys( obj1, obj2 );
pending…
extend_names
extend_names( obj1, obj2 );
pending…
extend_keys_while
extend_keys_while (obj1, obj2);
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