Closure vs Class (v3)

Revision 3 of this benchmark created on


Description

from https://twitter.com/trueadm/status/1707021262550094054

Setup

function plainobj(x) {
	return {
		get x() { return x; },
		set x(val) { x = val; }
	};
}

class Item {
	constructor(x) {
		this._x = x;
	}
	
	get x() { return this._x; }
	set x(val) { this._x = val; }
}

function newclass(x) {
	return new class {
		get x() { return x; }
		set x(val) { x = val; }
	}
}

function test(obj) {
	obj.x = Math.random();
	return obj.x;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Plain Object getter/setter (closure)
const temp = plainobj(1);
for (let j = 0; j < 1000000; j++) {
	test(temp);
}
ready
Class (private field)
const temp = new Item(1);
for (let j = 0; j < 1000000; j++) {
	test(temp);
}
ready
NewClass (closure)
const temp = newclass(1);
for (let j = 0; j < 1000000; j++) {
	test(temp);
}
ready

Revisions

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