Prototype vs Module pattern performance

JavaScript performance comparison

Revision 99 of this test case created by David

Info

Added testing of accessing long/short function names.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function TraditionalPrototypeClass() {
    }
   
    TraditionalPrototypeClass.prototype.foo = function() {
    var i = 1;
    i++;
    };
   
    TraditionalPrototypeClass.prototype.bar = function() {
    var i = 1;
    i++;
    };
   
    TraditionalPrototypeClass.prototype.barlonglongGreatFunctionNameToBeClear = function() {
    var i = 1;
    i++;
    };
   
    function ModulePatternClass() {
        this.foo = function() {
    var i = 1;
    i++;
        };
        this.bar = function() {
    var i = 1;
    i++;
        };
        this.barlonglongGreatFunctionNameToBeClear = function() {
    var i = 1;
    i++;
        };
    }
   
    var ModuleCachePatternClass = (function () {
        function foo() {
    var i = 1;
    i++;
        }
        function barlonglongGreatFunctionNameToBeClear() {
    var i = 1;
    i++;
        }  
        function bar() {
    var i = 1;
    i++;
        }  
        return function () {
            this.foo = foo;
            this.bar = bar;
            this.bar2 = barlonglongGreatFunctionNameToBeClear;
            this.barlonglongGreatFunctionNameToBeClear = bar;
            this.barlonglongGreatFunctionNameToBeClear2 = barlonglongGreatFunctionNameToBeClear;
        };
    }());
   
    var standardObject = {
        foo: function(){
    var i = 1;
    i++;
        },
        barlonglongGreatFunctionNameToBeClear: function(){
    var i = 1;
    i++;
        },
        bar: function(){
    var i = 1;
    i++;
        }
    };
};
</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
Prototype short
var o = new TraditionalPrototypeClass();
o.bar();
o.foo();
pending…
Prototype long
var o = new TraditionalPrototypeClass()
o.barlonglongGreatFunctionNameToBeClear();
o.foo();
pending…
object
var o = standardObject;
o.bar();
o.foo();
pending…
object long
var o = standardObject;
o.barlonglongGreatFunctionNameToBeClear();
o.foo();
pending…
short to long
var o = new ModuleCachePatternClass()
o.bar2();
o.foo();
pending…
long to short
var o = new ModuleCachePatternClass()
o.barlonglongGreatFunctionNameToBeClear();
o.foo();
pending…
long
var o = new ModuleCachePatternClass()
o.barlonglongGreatFunctionNameToBeClear2();
o.foo();
pending…
short
var o = new ModuleCachePatternClass()
o.bar();
o.foo();
pending…
Module long
var o = new ModulePatternClass()
o.barlonglongGreatFunctionNameToBeClear();
o.foo();
pending…
Module short
var o = new ModulePatternClass()
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