JS classes vs plain objects vs factory vs Object.create

Benchmark created on


Setup

class Class1
{
	x = 0;
	y = 0;
	
	constructor(x, y)
	{
		this.x = x;
		this.y = y;
	}
}

function factory1(x, y)
{
	return {
		x: x,
		y: y,
	};
}

const REPEATS_COUNT = 100000;

Test runner

Ready to run.

Testing in
TestOps/sec
Class
for (let i = 0; i < REPEATS_COUNT; i++)
{
	const instance = new Class1(5, 6);
}
ready
Factcory
for (let i = 0; i < REPEATS_COUNT; i++)
{
	const instance = factory1(5, 6);
}
ready
Plain object
for (let i = 0; i < REPEATS_COUNT; i++)
{
	const instance = {
		x: 5,
		y: 6,
	};
}
ready
Object with prototype
for (let i = 0; i < REPEATS_COUNT; i++)
{
	const instance = {
		x: 5,
		y: 6,
		__proto__: Class1.prototype,
	};
}
ready
Object create
for (let i = 0; i < REPEATS_COUNT; i++)
{
	const instance = Object.create(
		Class1.prototype,
		{
			x: {
				writable: true,
				configurable: true,
				enumerable: true,
				value: 5,
			},
			y: {
				writable: true,
				configurable: true,
				enumerable: true,
				value: 6,
			},
		}
	);
}
ready

Revisions

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