Prototype vs Module pattern performance

JavaScript performance comparison

Revision 108 of this test case created


Removed iterations in tests, because jsperf already does that for us. And renamed variables to be more meaningful to us humans. And other cosmetic changes.

The most important thing to remember is to use the right tool for the job. All these tests do is reference an object with a complex memory allocation. When you don't need something fancy, you're better off using a regular old object

Preparation code

Benchmark.prototype.setup = function() {
    function TraditionalPrototypeClass() { } = function () { return Math.random(); }; = function () { return Math.random(); };
    var standardObject = {
        foo: function () {
                return Math.random();
        bar: function () {
                return Math.random();
    var moduleObject = (function () {
        return {
                foo: function () { return Math.random(); },
                bar: function () { return Math.random(); }
    } ());
    var moduleCachedObject = (function () {
        function foo() { return Math.random(); }
        function bar() { return Math.random(); }
        return {
                foo: foo,
                bar: bar
    } ());

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
var o = new TraditionalPrototypeClass();;
Use the right tool for the job
var s = standardObject;;;
var s = moduleObject;;;
var s = moduleCachedObject;;;

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment