Closure vs ES6 class

Benchmark created on


Setup

class X {
	total = 0;
	
	y(x) {
		this.total += x;
	}
	
	mytotal() {
		return this.total;	
	}
}

function A() {
	
	let total = 0;
	
	this.b = function(x) {
		total += x;
	}
	
	this.mytotal = function() {
		return total;
	}
	
}

Test runner

Ready to run.

Testing in
TestOps/sec
ES6 class
const x = new X();

for (let i=0; i<1000000; i++) {
	x.y(i);
}

console.log(x.mytotal());
ready
Closure
const a = new A();

let total = 0;
for (let i=0; i<1000000; i++) {
	a.b(i);
}

console.log(a.mytotal());
ready

Revisions

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