How fast is Math.max

Benchmark created on


Description

Does the fact that this method accept a list of arguments instead of array affects its performance?

Setup

const fill = (N) => {
	const n = 0.01 * N,
		nN = N - n,
		MN = 1000,
		kM = MN/N,
		km = nN*MN/N/N;
	return [...new Array(N)]
		.map((_, i) => i)
		.map(i => [i*kM, i*km])
		.map(([M, m]) => m+Math.random(M-m));
}


const data = fill(100000)

Test runner

Ready to run.

Testing in
TestOps/sec
Math.max
const max = Math.max(...data);
ready
reduce
const max = data.reduce((max, val) => val > max ? val : max);
ready
for
let max = data[0];

for (let i = 1; i < data.length; i++) {
	if (data[i] > max) {
		max = data[i];
	}
}
ready

Revisions

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