Prototype vs Module pattern performance (z) (v132)

Revision 132 of this benchmark created by Anzor on


Description

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

Setup

function TraditionalPrototypeClass() {
    }
    
    TraditionalPrototypeClass.prototype.foo = function() {
    };
    
    TraditionalPrototypeClass.prototype.bar = function() {
    };
    
    function ModulePatternClass() {
        this.foo = function() {
        };
        
        this.bar = function() {
        };
    }
    
    var ModuleCachePatternClass = (function () {
        function foo() {
        }
        
        function bar() {
        }
        
        return function () {
            this.foo = foo;
            this.bar = bar;
        };
    }());
    
    var standardObject = {
        foo: function(){
        },
        bar: function(){
        }
    };
    
    var one = new TraditionalPrototypeClass()
    var two = new ModulePatternClass()
    var three = new ModuleCachePatternClass()
    var four = Object.create(standardObject);

Test runner

Ready to run.

Testing in
TestOps/sec
Prototypal
one.bar;
one.foo;
ready
Module pattern
two.bar;
two.foo;
ready
Module pattern with cached functions
three.bar;
three.foo;
ready
Use the right tool for the job 2
four.bar;
four.foo;
ready

Revisions

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