Prototype vs. this (v67)

Revision 67 of this benchmark created on


Setup

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

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

Test runner

Ready to run.

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

Revisions

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