Prototype vs. this (v59)

Revision 59 of this benchmark created by Walter Wang on


Setup

function T(x, y, z) {
      this.x = x | 0;
      this.y = y | 0;
      this.z = z | 0;
      this.result = 0 | 0;
    }
    
    T.prototype.myMethod = function(i) {
      this.result = this.x | this.y | this.z | i;
    };
    
    function Tt(x, y, z) {
      this.x = x | 0;
      this.y = y | 0;
      this.z = z | 0;
      this.result = 0 | 0;
      this.myMethod = function(i) {
        this.result = this.x | this.y | this.z | i;
      };
    }

Test runner

Ready to run.

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

Revisions

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