Prototype vs. this (v33)

Revision 33 of this benchmark created on


Setup

function T() {}
    
    T.prototype.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey T!');
      }
    };
    
    function Tt() {
      this.myMethod = function(i) {
        if (i == 20000) {
          console.log('Hey Tt!');
        }
      };
    }
    
    function Dd() {
      return this.prototype = {
        myMethod: function(i) {
          if (i == 20000) {
            console.log('Hey Tt!');
          }
        }
      };
    }
    
    var Oo = {};
    Oo.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey Oo!');
      }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Using prototype
  var x = new T();
  for (var i = 1; i <= 20000; i++) {
    x.myMethod(i);
  }
ready
Using this
var x = new Tt();
for (var i = 1; i <= 20000; i++) {
  x.myMethod(i);
}
ready
Using thisprototype
var x = new Dd();
for (var i = 1; i <= 20000; i++) {
  x.myMethod(i);
}
ready
Using literal
for (var i = 1; i <= 20000; i++) {
  Oo.myMethod(i);
}
ready

Revisions

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