Two Filter vs Reduce

Benchmark created on


Setup

var addons = [];

for (let index = 0; index < 1000; index++) {
  var innerPrice = {
    amount: Math.round(Math.random() * 10),
    test: 'x',
    more: 'data',
    just: 'included',
    for: 'fun',
  };
  var price = (index % 2) ? {
    recurring: { ...innerPrice }
  } : {
    upfront: { ...innerPrice }
  };

  addons.push({
    name: index.toString(),
    price: { ...price },
    id: index,
    data: {
      just: 'some',
      random: 'data',
    }
  })
}

Test runner

Ready to run.

Testing in
TestOps/sec
Two Filter
const monthly = addons.filter((addon) =>  !!addon.price.recurring);

const upfront = addons.filter((addon) =>  !!addon.price.upfront);
ready
Reduce
const { monthly, upfront } = addons.reduce(
  (acc, product) => {
    if (product.price.recurring) {
      acc.monthly.push(product);
      return acc;
    }
    acc.upfront.push(product);
    return acc;
  },
  {
    monthly: [],
    upfront: [],
  },
);
ready

Revisions

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