Class vs Factory (v2)

Revision 2 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Class
class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

for(let i = 0; i < 100000; i++) {
	new Rectangle(i, i);
}
ready
Factory
function RectangleFactory(height, width) {
	return { height, width }
}

for(let i = 0; i < 100000; i++) {
	RectangleFactory(i, i);
}
ready
Class (Defaulted)
class Rectangle {
  height = 0;
  width = 0;
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

for(let i = 0; i < 100000; i++) {
	new Rectangle(i, i);
}
ready
Factory (Naive)
function RectangleFactory(height, width) {
	let obj = {};
	obj.height = height;
	obj.width = width;
	return obj;
}

for(let i = 0; i < 100000; i++) {
	RectangleFactory(i, i);
}
ready

Revisions

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