Class vs Factory

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 < 10000; i++) {
	new Rectangle(i, i);
}
ready
Factory
function RectangleFactory(height, width) {
	return { height, width }
}

for(let i = 0; i < 10000; 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 < 10000; i++) {
	new Rectangle(i, i);
}
ready

Revisions

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