closure vs {fn, data} (v4)

Revision 4 of this benchmark created on


Preparation HTML

<p>Just how <em>slow</em> are closures?</p>

Setup

function fn(data) {
	if (data.x < 10) {
		return data.y * 30 / (data.x + 10);
	}
	if (data.x < 20) {
		return (data.y / data.x)
	}
	return data.y * 100 / (data.x / 10)
}

let dataList = new Array(100)

for (let i = 0; i < dataList.length; i++) {
	let x = Math.ceil(Math.random() * 50)
	let y = Math.ceil(Math.random() * 200)
	dataList[i] = { x, y }
}

Test runner

Ready to run.

Testing in
TestOps/sec
with closures
function doit(fnParam) {
	fnParam()
}

for (let i = 0; i < dataList.length; i++) {
	(() => fn(dataList[i]))()
	}
ready
fn, data
function doit(bundle) {
	bundle.fn(bundle.data)
}

for (let i = 0; i < dataList.length; i++) {
	let data = dataList[i]
	doit({fn, data})
}
ready
foreach
dataList.forEach((x)=>fn(x));
ready
for each without closure
dataList.forEach(fn)
ready

Revisions

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