Instance vs. prototype methods (v5)

Revision 5 of this benchmark created on


Description

Evaluate difference adding external functions as methods to object instances or the prototype.

Setup

function test1() {
	return "test1: " + this.name;
}

function test2() {
	return "test2: " + this.name;
}

function test3() {
	return "test3: " + this.name;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype Methods
class MyClass {
	constructor() {
		this.name = "proto";
	}
}
MyClass.prototype.test1 = test1;
MyClass.prototype.test2 = test2;
MyClass.prototype.test3 = test3;

const mc = new MyClass();
mc.test1();
mc.test2();
mc.test3();
ready
Instance Methods
class MyClass {
	constructor() {
		this.name = "instance";
		this.test1 = test1.bind(this);
		this.test2 = test2.bind(this);
		this.test3 = test3.bind(this);
	}
}

const mc = new MyClass();
mc.test1();
mc.test2();
mc.test3();
ready
Methods in class definition
class MyClass {
	constructor() {
		this.name = "unified"
	}
	test1() {
		return "test1: " + this.name;
	}
	test2() {
		return "test2: " + this.name;
	}
	test3() {
		return "test3: " + this.name;
	}
}

const mc = new MyClass();
mc.test1();
mc.test2();
mc.test3();
ready

Revisions

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