Prototype vs Module pattern performance

JavaScript performance comparison

Revision 92 of this test case created

Preparation code

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