Prototype vs. this (v17)

Revision 17 of this benchmark created on


Description

Moving the object instantiation to the prep block.

Setup

function T() {
    }
    
    T.prototype.myMethod = function(i) {
      if (i == 20000) {
        console.log('Hey T!');
      }
    };
    var t = new T();
    
    function Tt() {
      this.myMethod = function(i) {
        if (i == 20000) {
          console.log('Hey Tt!');
        }
      };
    }
    var tt = new Tt();
    var foo = {};
    foo.myMethod = 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++) {
  t.myMethod(i);
}
ready
Using this
for (var i = 1; i <= 20000; i++) {
  tt.myMethod(i);
}
ready
Using function
for (var i = 1; i <= 20000; i++) {
  foo.myMethod(i);
}
ready

Revisions

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