Prototype vs Module pattern performance

JavaScript performance comparison

Revision 129 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Klass1 = function() {}
    Klass1.prototype.a = function() {
      log('thislog');
    }
    Klass1.prototype.b = function() {
      log('thislog');
    }
    Klass1.prototype.c = function() {
      log('thislog');
    }
    Klass1.prototype.d = function() {
      log('thislog');
    }
    Klass1.prototype.e = function() {
      log('thislog');
    }
    Klass1.prototype.f = function() {
      log('thislog');
    }
    Klass1.prototype.g = function() {
      log('thislog');
    }
    Klass1.prototype.h = function() {
      log('thislog');
    }
    Klass1.prototype.i = function() {
      log('thislog');
    }
   
    Klass2 = function() {
      var a = function() {
            log('thislog');
          },
          b = function() {
            log('thislog');
          },
          c = function() {
            log('thislog');
          },
          d = function() {
            log('thislog');
          },
          e = function() {
            log('thislog');
          },
          f = function() {
            log('thislog');
          },
          g = function() {
            log('thislog');
          },
          h = function() {
            log('thislog');
          },
          i = function() {
            log('thislog');
          };
   
      return {a:a,b:b,c:c,d:d,e:e,f:f,g:g,h:h,i:i};
    }
   
   
    var aFunction = function() {
      log('thislog');
    };
    var bFunction = function() {
      log('thislog');
    };
    var cFunction = function() {
      log('thislog');
    };
    var dFunction = function() {
      log('thislog');
    };
    var eFunction = function() {
      log('thislog');
    };
    var fFunction = function() {
      log('thislog');
    };
    var gFunction = function() {
      log('thislog');
    };
    var hFunction = function() {
      log('thislog');
    };
    var iFunction = function() {
      log('thislog');
    };
    Klass3 = function() {
      return {a:aFunction,b:bFunction,c:cFunction,d:dFunction,e:eFunction,f:fFunction,g:gFunction,h:hFunction,i:iFunction};
    };
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Prototypal
var i = 1000,
    objs = [];
while (i--) {
  var o = new Klass1()
  objs.push(new Klass1());
  o.a;
  o.b;
}
pending…
Module pattern
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass2()
  objs.push(Klass2());
  o.a;
  o.b;
}
pending…
Module pattern with cached functions
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass3()
  objs.push(Klass3());
  o.a;
  o.b;
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment