... with big arrays

Benchmark created on


Setup

const a = [];
const L = 1000000;
let i = 0;
while (i < L) {
	const v = ((i / L) > 0.5) ? 1 : -1;
	a.push(v * i);
	i++
}

Teardown

a.length = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Math.max and spread
Math.max(...a);
ready
Max with for
let out = -Infinity;
for (let i = 0; i < a.length; i++) {
	if (a[i] > out) out = a[i];
}
ready
Max with reduce
const max = a.reduce((acc, currentValue) => {
    return currentValue > acc ? currentValue : acc;
}, -Infinity);
ready

Revisions

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