Prototype vs Module pattern performance

JavaScript performance comparison

Revision 128 of this test case created by mog

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>
function TraditionalPrototypeClass() {}

TraditionalPrototypeClass.prototype.foo = function() {};

TraditionalPrototypeClass.prototype.bar = function() {};


function ModulePatternClass() {
  this.foo = function() {};

  this.bar = function() {};
}


function ModulePatternClassIndirect() {
  function foo() {};

  function bar() {};
  return {
    foo: foo,
    bar: bar
  };
}

//no var, so we can access it in the tests
var testProto = new TraditionalPrototypeClass(),
  testModule = new ModulePatternClass(),
  testModuleIndirect = ModulePatternClassIndirect();
</script>
<script>
Benchmark.prototype.setup = function() {
   
   
};
</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
var o = new TraditionalPrototypeClass()
o.bar;
o.foo;
pending…
Module pattern
var o = new ModulePatternClass()
o.bar;
o.foo;
pending…
Module pattern Indirect
var o = new ModulePatternClassIndirect()
o.bar;
o.foo;
pending…
Retrieval - Proto
testProto.bar();
testProto.foo();
pending…
Retrieval - Module
testModule.bar();
testModule.foo();
pending…
Retrieval - Module Pattern
testModuleIndirect.bar();
testModuleIndirect.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:

1 comment

mog (revision owner) commented :

Testing another form of module pattern, that uses return instead of scoping on this.

Add a comment