Does pre-defining properties help?

Benchmark created on


Setup

class A1 {
	#a;
	#b;
	constructor(a, b) {
		this.#a = a;
		this.#b = b;
	}
	getA() {return this.a;}
	getB() {return this.b;}
}

class A2 {
	constructor(a, b) {
		this._a = a;
		this._b = b;
	}
	getA() {return this._a;}
	getB() {return this._b;}
}

Test runner

Ready to run.

Testing in
TestOps/sec
Test 1
let sum = 0;
for(let j = 0; j < 1000; j++) {
	let obj = new A1(j, 2);
	for(let i = 0; i < 1000; i++) {
		sum += obj.getA() + obj.getB();
	}
}
TrueSum = sum;
ready
Test 2
let sum = 0;
for(let j = 0; j < 1000; j++) {
	let obj = new A2(j, 2);
	for(let i = 0; i < 1000; i++) {
		sum += obj.getA() + obj.getB();
	}
}
TrueSum = sum;
ready

Revisions

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