Prototype vs Module pattern performance

JavaScript performance comparison

Revision 44 of this test case created by Grant Kiely

Preparation code

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