Prototype vs. Object.create (v2)

Revision 2 of this benchmark created by cHao on


Setup

if (typeof Object.create !== 'function') {
      Object.create = function(o) {
        function F() {}
        F.prototype = o;
        return new F();
      };
    }
    
    var Foo = {
      bar: function() {
        return this.num;
      },
      multi: function(by) {
        return this.num * by;
      }
    };
    
    var Foo2 = function(num) {
      this.num = num;
    }
    
    var Foo3 = function(num) {
      this.num = num;
    }
    
    Foo2.prototype = {
      bar: function() {
        return this.num;
      },
      multi: function(by) {
        return this.num * by;
      }
    };
    
    Foo3.prototype.bar = function() {
      return this.num;
    }
    
    Foo3.prototype.multi = function(by) {
      return this.num * by;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Object.create
var foo = Object.create(Foo, {
  num: {
    value: 1
  }
});
foo.bar();
foo.multi(2);
ready
Regular Prototype
var foo = new Foo2(1);
foo.bar();
foo.multi(2);
ready
Regular Prototype 2
var foo = new Foo3(1);
foo.bar();
foo.multi(2);
ready
Object.create minus args
var foo = Object.create(Foo);
foo.num = 1;
foo.bar();
foo.multi(2);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by James Simpson on
  • Revision 2: published by cHao on