closure vs {fn, data}

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++) {
	doit(() => 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

Revisions

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