Prototype vs. this (v62)

Revision 62 of this benchmark created by endro 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!');
        }
      };
    }

Test runner

Ready to run.

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

Revisions

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