Prototype vs Module pattern performance (v44)

Revision 44 of this benchmark created by Grant Kiely on


Setup

Klass1 = function() {}
    Klass1.prototype.foo = function() {
      log('foo');
    }
    Klass1.prototype.bar = function() {
      log('bar');
    }
    var o1 = new Klass1();
    
    Klass2 = function() {
        var p ={};
        p.foo = function() {
                log('foo');
        };
        p.bar= function() {
                log('bar');
      };
      return p;
    }
    var o2 = new Klass2();
    
    var Klass3 = (function(){
        var p ={};
        p.foo = function() {
                log('foo');
        };
        p.bar= function() {
                log('bar');
      };
      return p;
    })();

Test runner

Ready to run.

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

Revisions

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