Prototype vs. this (v19)

Revision 19 of this benchmark created on


Setup

function T() {
    }
    
    T.prototype.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey T!');
      }
    };
    
    T.prototype.myMethod2 = function(i) {
      if (i == 20000) {
        console.log('Hey T!');
      }
    };
    
    function Tt() {
      this.myMethod = function(i) {
        if (i == 20000) {
          console.log('Hey Tt!');
        }
      };
    
      this.myMethod2 = function(i) {
        if (i == 20000) {
          console.log('Hey Tt!');
        }
      };
    }

Test runner

Ready to run.

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

Revisions

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