Prototype vs. this (v52)

Revision 52 of this benchmark created on


Description

Moving the object instantiation outside prep block.

Setup

function T() {
      this.name = 'T';
    }
    
    T.prototype.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey ' + this.name);
      }
    };
    
    function Tt() {
      this.name = 'Tt';
      this.myMethod = function(i) {
        if (i == 20000) {
          console.log('Hey ' + this.name);
        }
      };
    }
    
    
    function fact() {
      var inst = {};
      inst.name = 'fact';
      inst.myMethod = myMethod;
    
      function myMethod (i) {
        if (i == 20000) {
          console.log('Hey ' + inst.name);
        }
      };
    
      return inst;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Using prototype
 var t = new T();
 for (var i = 1; i <= 20000; i++) {
   t.myMethod(i);
 }
ready
Using this
var tt = new Tt();
for (var i = 1; i <= 20000; i++) {
  tt.myMethod(i);
}
ready
Factory
var ttt = new fact();
for (var i = 1; i <= 20000; i++) {
  ttt.myMethod(i);
}
ready
Factory ( no new )
var tttt = fact();
for (var i = 1; i <= 20000; i++) {
  tttt.myMethod(i);
}
ready

Revisions

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