Prototype vs Module pattern performance

JavaScript performance comparison

Revision 10 of this test case created

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
Klass = function() {}
Klass.prototype.foo = function() {
  alert('foo');
}
Klass.prototype.bar = function() {
  alert('bar');
}
Klass.prototype.foo1 = function() {
  alert('foo');
}
Klass.prototype.bar1 = function() {
  alert('bar');
}

Klass.prototype.foo2 = function() {
  alert('foo');
}
Klass.prototype.bar2 = function() {
  alert('bar');
}

Klass.prototype.foo3 = function() {
  alert('foo');
}
Klass.prototype.bar3 = function() {
  alert('bar');
}
Klass.prototype.foo4 = function() {
  alert('foo');
}
Klass.prototype.bar4 = function() {
  alert('bar');
}



var i = 20,
    objs = [];
while (i--) {
  objs.push(new Klass());
}
pending…
Module pattern
Klass = function() {
  var foo = function() {
      alert('foo');
      },
      bar = function() {
      alert('bar');
      },
      foo1 = function() {
      alert('foo');
      },
      bar1 = function() {
      alert('bar');
      },
      foo2 = function() {
      alert('foo');
      },
      bar2 = function() {
      alert('bar');
      },
      foo3 = function() {
      alert('foo');
      },
      bar3 = function() {
      alert('bar');
      },
      foo4 = function() {
      alert('foo');
      },
      bar4 = function() {
      alert('bar');
      };

  return {
    foo: foo,
    bar: bar,
    foo1: foo1,
    bar1: bar1,
    foo2: foo2,
    bar2: bar2,
    foo3: foo3,
    bar3: bar3,
    foo4: foo4,
    bar4: bar4
  }
}

var i = 20,
    objs = [];
while (i--) {
  objs.push(new Klass());
}
pending…
Go
Klass = function() {
  this.foo = function() {
    alert('foo');
  };
  this.bar = function() {
    alert('bar');
  };
  this.foo1 = function() {
    alert('foo');
  };
  this.bar1 = function() {
    alert('bar');
  };
  this.foo2 = function() {
    alert('foo');
  };
  this.bar2 = function() {
    alert('bar');
  };
  this.foo3 = function() {
    alert('foo');
  };
  this.bar3 = function() {
    alert('bar');
  };
  this.foo4 = function() {
    alert('foo');
  };
  this.bar4 = function() {
    alert('bar');
  };
}

var i = 20,
    objs = [];
while (i--) {
  objs.push(new Klass());
}
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