Module pattern vs. Prototype

JavaScript performance comparison

Test case created by Joshy

Info

Performance comparison between two approaches.

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Prototype
Klass = function(name) {
  this.name = name;
  this.foo();
  this.bar();
}
Klass.prototype.foo = function() {
  alert(this.name);
}
Klass.prototype.bar = function() {
  alert(this.name + ' bar');
}

var i = 1,
    objs = [];
while (i--) {
  objs.push(new Klass('John Doe ' + i));
}
pending…
Module pattern
var Klass = (function(module) {

  var name = '';

  module = {
    init: function(name) {
      module.name = name;
      module.foo();
      module.bar();
    },
    foo: function() {
      alert(module.name);
    },
    bar: function() {
      alert(module.name + ' bar');
    }
  }

  return module;
})(Klass || {});

var i = 1,
    objs = [];
while (i--) {
  objs.push(Klass.init('John Doe ' + i));
}
pending…

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

Compare results of other browsers

0 comments

Add a comment