Closure vs Class

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;
}

const ins_plainobj = plainobj(1);
const ins_class = new Item(1);
const ins_newclass = newclass(1);

Test runner

Ready to run.

Testing in
TestOps/sec
Plain Object getter/setter (closure)
test(ins_plainobj);
ready
Class (private field)
test(ins_class);
ready
NewClass (closure)
test(ins_newclass);
ready

Revisions

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