Prototype vs Module pattern performance

JavaScript performance comparison

Revision 116 of this test case created

Info

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

 
<script>
Benchmark.prototype.setup = function() {
    function TraditionalPrototypeClass() {
    }
   
    TraditionalPrototypeClass.prototype.foo = function() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    };
   
    TraditionalPrototypeClass.prototype.bar = function() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    };
   
    function ModulePatternClass() {
        this.foo = function() {
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        };
       
        this.bar = function() {
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        };
    }
   
    var ModuleCachePatternClass = (function () {
        function foo() {
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        }
       
        function bar() {
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        }
       
        return function(){
            this.foo = foo;
            this.bar =bar;
        };
    }());
   
    var standardObject = {
        foo: function(){
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        },
        bar: function(){
            var test = 5;
            test = test*test*test;
            test--;
            test = 502;
        }
    };
};
</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
function TraditionalPrototypeClass() {
}

TraditionalPrototypeClass.prototype.foo = function() {
    var test = 5;
    test = test*test*test;
    test--;
    test = 502;
};

TraditionalPrototypeClass.prototype.bar = function() {
    var test = 5;
    test = test*test*test;
    test--;
    test = 502;
};

var o = new TraditionalPrototypeClass()
o.bar;
o.foo;
pending…
Module pattern
function ModulePatternClass() {
    this.foo = function() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    };
   
    this.bar = function() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    };
}

var o = new ModulePatternClass()
o.bar;
o.foo;
pending…
Module pattern with cached functions
var ModuleCachePatternClass = (function () {
    function foo() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    }
   
    function bar() {
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    }
   
    return function(){
        this.foo = foo;
        this.bar =bar;
    };
}());

var o = new ModuleCachePatternClass()
o.bar;
o.foo;
pending…
Use the right tool for the job
var standardObject = {
    foo: function(){
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    },
    bar: function(){
        var test = 5;
        test = test*test*test;
        test--;
        test = 502;
    }
};

var o = standardObject;
o.bar;
o.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