Prototype vs. this (v53)

Revision 53 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!');
        }
      };
    }
    arr1=[];
    arr2=[];
    for (var i = 0; i <= 20000; i++) {
      arr1.push(new T());
      arr2.push(new Tt());
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Using prototype
for (var i = 0; i < arr1.length; i++) {
  arr1[i].myMethod(i);
}
ready
Using this
for (var i = 0; i < arr2.length; i++) {
  arr2[i].myMethod(i);
}
ready

Revisions

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