Prototype vs Module pattern performance (v92)

Revision 92 of this benchmark created on


Setup

assertEqual = function(a, b) {
      if (a !== b) {
        throw new Error('values are not equal');
      }
    }
    Klass1 = function() {}
    Klass1.prototype.foo = function() {
      return this;
    }
    Klass1.prototype.bar = function() {
      return this;
    }
    
    Klass2 = function() {
      var foo = function() {
          return this;
          },
          bar = function() {
          return this;
          };
    
      return {
        foo: foo,
        bar: bar
      }
    }
    
    
    var FooFunction = function() {
        return this;
        };
    var BarFunction = function() {
        return this;
        };
    
    Klass3 = function() {
      return {
        foo: FooFunction,
        bar: BarFunction
      }
    }

Test runner

Ready to run.

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

Revisions

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