Once-off prototype vs this

Benchmark created by Davyd McColl on


Setup

function createThingUsingPrototype() {
		function Thing() {
		}
		  
		Thing.prototype.myMethod = function(i) {
			if (i == 20000) {
			  console.log('Hey T!');
			}
		};
		return new Thing();
	}

  
	function createThingUsingThis() {
		var Thing = function() {
			this.myMethod = function(i) {
			   if (i === 20000) {
				console.log("Hey from 'using this'");
			   }
			}
		}
		return new Thing();
	 }

Test runner

Ready to run.

Testing in
TestOps/sec
using prototype
for (var i = 0; i < 20000; i++) {
  var thing = createThingUsingPrototype();
  thing.myMethod(i);
}
ready
using this
for (var i = 0; i < 20000; i++) {
  var thing = createThingUsingThis();
  thing.myMethod(i);
}
ready

Revisions

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

  • Revision 1: published by Davyd McColl on