Prototype vs Module pattern performance (v227)

Revision 227 of this benchmark created by anri on


Setup

log = function(val){
    return val+val;
    }
    Klass1 = function() {}
    Klass1.prototype.foo = function() {
      log('foo');
    }
    Klass1.prototype.bar = function() {
      log('bar');
    }
    k1 = new Klass1();
    
    Klass2 = function() {
      var foo = function() {
            log('foo');
          },
          bar = function() {
            log('bar');
          };
    
      return {foo: foo, bar: bar}
    }
    
    k2 = Klass2();
    
    
    var FooFunction = function() {
      log('foo');
    };
    var BarFunction = function() {
      log('bar');
    };
    
    Klass3 = function() {
      return {foo: FooFunction, bar: BarFunction}
    }
    
    k3= Klass3();

Test runner

Ready to run.

Testing in
TestOps/sec
Prototypal
var i = 1000;
while (i--) {
  k1.bar();
  k1.foo();
}
ready
Module pattern
var i = 1000;
while (i--) {
  k2.bar();
  k2.foo();
}
ready
Module pattern with cached functions
var i = 1000;
while (i--) {
  k3.bar();
  k3.foo();
}
ready

Revisions

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