Array Methods vs Iterative

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Array Methods
const numbers = Array.from({ length: 10_000 }).map(() => Math.random());

const result =
  numbers
    .map(n => Math.round(n * 10))
    .filter(n => n % 2 === 0)
    .reduce((a, n) => a + n, 0);
ready
Iterative Method
const numbers = Array.from({ length: 10_000 }).map(() => Math.random());

let result = 0
for (let i = 0; i < numbers.length; i++) {
  let n = Math.round(numbers[i] * 10)
  if (n % 2 !== 0) continue
  result = result + n
}
ready
Better Array Methods
const numbers = Array.from({ length: 10_000 }).map(() => Math.random());

const result =
  numbers.reduce((acc, current) => {
    const value = Math.round(current * 10);

    acc += value % 2 === 0 ? current : 0;

    return acc;
  })
ready

Revisions

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