reduce-vs-for-of

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
getMax with reduce
const samples = Array.from({ length: 100000 }, (_e, index) => index)

const getMax = (arr) =>
  arr.reduce(
    ([min, max], val) => [Math.min(min, val), Math.max(max, val)],
    [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
  )[1]
 
const max = getMax(samples)
console.log(max)
ready
getMax with for of
const samples = Array.from({ length: 100000 }, (_e, index) => index)

function getMax(numbers) {
  let max = Number.NEGATIVE_INFINITY

  for (const number of numbers) {
    if (number > max) {
      max = number
    }
  }

  return max
}

const max = getMax(samples)
console.log(max)
ready

Revisions

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