How fast is Math.max (v2)

Revision 2 of this 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);

const max2MathMax = (a, b) => Math.max(a,b);
const max2Tern = (a, b) => a > b ? a : b;

function myReduce(arr, f) {
	let acc = f(arr[0], arr[1]);
	for (let i = 1; i < arr.length; i++) {
		acc = f(acc, arr[i]);
	}
	return acc;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Math.max
const max = Math.max(...data);
ready
reduce + ternary
const max = data.reduce(max2Tern);
ready
for + ternary
let max = data[0];

for (let i = 1; i < data.length; i++) {
	max = max2Tern(data[i], max);
}
ready
reduce + Math.max
const max = data.reduce(max2MathMax);
ready
for + Math.max
let max = data[0];

for (let i = 1; i < data.length; i++) {
	max = max2MathMax(data[i], max);
}
ready
my reduce + ternary
const max = myReduce(data, max2Tern);
ready

Revisions

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