Prototype vs Module pattern performance (v100)

Revision 100 of this benchmark created by Ryan on


Description

  • Changed the behavior of the dummy methods.
  • Removed the "object" tests since they do not allocate memory.

Setup

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;
        };
    }());

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype Short Name
var o = new TraditionalPrototypeClass();
o.foo();
ready
Prototype Long Name
var o = new TraditionalPrototypeClass()
o.toFooBarOrNotToFooBarQuestion();
 
ready
Short to Long Name
var o = new ModuleCachePatternClass()
o.bar();
 
ready
Long to Short Name
var o = new ModuleCachePatternClass()
o.toFooBarOrNotToFooBarThatIsTheQuestion();
ready
Long Name
var o = new ModuleCachePatternClass()
o.toFooBarOrNotToFooBarThatIsTheQuestion();
 
ready
Short Name
var o = new ModuleCachePatternClass()
o.foo();
ready
Module Long Name
var o = new ModulePatternClass()
o.toFooBarOrNotToFooBarQuestion();
 
ready
Module Short Name
var o = new ModulePatternClass()
o.foo();
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.