Prototype vs Module pattern performance

JavaScript performance comparison

Revision 100 of this test case created by Ryan

Info

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var random = Math.random();
   
    // TraditionalPrototypeClass
    function TraditionalPrototypeClass() {
    }
    TraditionalPrototypeClass.prototype.foo = function() {
        return random + 1;
    };
    TraditionalPrototypeClass.prototype.bar = function() {
        return random + 1;
    };
    TraditionalPrototypeClass.prototype.toFooBarOrNotToFooBarQuestion = function() {
        return random + 1;
    };
   
    // ModulePatternClass
    function ModulePatternClass() {
        this.foo = function() {
            return random + 1;
        };
        this.bar = function() {
            return random + 1;
        };
        this.toFooBarOrNotToFooBarQuestion = function() {
            return random + 1;
        };
    }
   
    // ModuleCachePatternClass
    var ModuleCachePatternClass = (function () {
        function foo() {
            return random + 1;
        }
        function bar() {
            return random + 1;
        }  
        function toFooBarOrNotToFooBarQuestion() {
            return random + 1;
        }  
        return function () {
            this.foo = foo;
            this.bar = toFooBarOrNotToFooBarQuestion;
            this.toFooBarOrNotToFooBarThatIsTheQuestion = bar;
        };
    }());
   
};
</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 Name
var o = new TraditionalPrototypeClass();
o.foo();
pending…
Prototype Long Name
var o = new TraditionalPrototypeClass()
o.toFooBarOrNotToFooBarQuestion();
 
pending…
Short to Long Name
var o = new ModuleCachePatternClass()
o.bar();
 
pending…
Long to Short Name
var o = new ModuleCachePatternClass()
o.toFooBarOrNotToFooBarThatIsTheQuestion();
pending…
Long Name
var o = new ModuleCachePatternClass()
o.toFooBarOrNotToFooBarThatIsTheQuestion();
 
pending…
Short Name
var o = new ModuleCachePatternClass()
o.foo();
pending…
Module Long Name
var o = new ModulePatternClass()
o.toFooBarOrNotToFooBarQuestion();
 
pending…
Module Short Name
var o = new ModulePatternClass()
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