Instance vs. prototype methods

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";
	}
}
Object.assign(MyClass.prototype, {test1, test2, 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

Revisions

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