Prototype vs Module pattern performance

JavaScript performance comparison

Revision 19 of this test case created by Witali

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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)}
    }
};
</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(i)
  objs.push(new Klass1(i));
  o.bar;
  o.foo;
}
pending…
Module pattern
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass2(i)
  objs.push(Klass2(i));
  o.bar;
  o.foo;
}
pending…
Module pattern with cached functions
var i = 1000,
    objs = [];
while (i--) {
  var o = Klass3(i)
  objs.push(Klass3(i));
  o.bar;
  o.foo;
}
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