Reduce vs for

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
reduce
function getArrayValueDifference(a, b) {
  let diff = 0;

  if (a.length === b.length) {
    return diff;
  }

  return (
    b.reduce((acc, curr) => acc + curr, 0) -
    a.reduce((acc, curr) => acc + curr, 0)
  );
}
ready
loop
function getArrayValueDifference3(a, b) {
  let diff = 0;
  if (a.length === b.length) {
    return diff;
  }
  for (let i = 0; i < b.length; i++) {
    diff += b[i];
  }
  for (let i = 0; i < a.length; i++) {
    diff -= a[i];
  }
  return diff;
}
ready

Revisions

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