Prototype vs Module pattern performance (v19)

Revision 19 of this benchmark created by Witali on


Setup

Klass1 = function(p) { this.p = p; }
    Klass1.prototype.foo = function() {
      log('foo' + this.p);
    }
    Klass1.prototype.bar = function() {
      log('bar' + this.p);
    }
    
    Klass2 = function(p) {
      var foo = function(p) {
            log('foo' + p);
          },
          bar = function(p) {
            log('bar' + p);
          };
    
      return {foo: foo, bar: bar}
    }
    
    
    var FooFunction = function() {
      log('foo' + this.p);
    };
    var BarFunction = function() {
      log('bar' + this.p);
    };
    
    Klass3 = function(p) {
      this.p = p;
      return {foo: FooFunction.bind(this), bar: BarFunction.bind(this)}
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Prototypal
var i = 1000,
    objs = [];
while (i--) {
  var o = new Klass1(i)
  objs.push(new Klass1(i));
  o.bar;
  o.foo;
}
ready
Module pattern
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass2(i)
  objs.push(Klass2(i));
  o.bar;
  o.foo;
}
ready
Module pattern with cached functions
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass3(i)
  objs.push(Klass3(i));
  o.bar;
  o.foo;
}
ready

Revisions

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