callback vs. destructuring assignment

Benchmark created on


Setup

function divmod1(a, b) {
	return [Math.trunc(a / b), ((a % b) + a)];
}
function divmod2(a, b, cb) {
	cb(Math.trunc(a / b), ((a % b) + a));
}
const vals = [...Array(1000).keys()].map(a => Math.floor(Math.random() * 1000));

Test runner

Ready to run.

Testing in
TestOps/sec
destructuring assignment
let out = vals.map((e,a,i) => {
	const o = {};
	[o.quotient, o.remainder] = divmod1(a[i], a[i+1]??2);
	return o;
});
ready
callback
let out = vals.map((e,a,i) => {
	const o = {};
	divmod2(a[i], a[i+1]??2, (q, r) => {
		o.quotient = q;
		o.remainder = r;
	});
	return o;
});
ready

Revisions

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