to filter or not to filter (v2)

Revision 2 of this benchmark created on


Setup

const cards = [
  {
    id: 1,
    type: 'cc',
    name: 'foobar'
  },
    {
    id: 1,
    type: 'cc',
    name: 'foobar'
  },
  {
    id: 2,
    type: 'cc',
    name: 'foobar'
  },
  {
    id: 3,
    type: 'cc',
    name: 'foobar'
  },
  {
    id: 4,
    type: 'cc',
    name: 'foobar'
  },
  {
    id: 5,
    type: 'paypal',
    name: 'foobar'
  },
  {
    id: 6,
    type: 'bank',
    name: 'foobar'
  }
]

Test runner

Ready to run.

Testing in
TestOps/sec
No filter
const findPaymentMethod = ({ paymentMethods, id, type }) => {
  const paymentMethod = paymentMethods.find((pm) => pm.id === id);
  if (type === undefined || paymentMethod?.type === type) {
    return paymentMethod;
  }
  return null;
};
findPaymentMethod({ paymentMethods: cards, id: 5, type: 'paypal' })
ready
Filter
const findPaymentMethod = ({ paymentMethods, id, type }) => {
  const paymentMethod = paymentMethods.filter((pm) => pm.id === id && pm.type === type)[0];
  if (paymentMethod) {
    return paymentMethod;
  }
  return null;
};
findPaymentMethod({ paymentMethods: cards, id: 5, type: 'paypal' })
ready

Revisions

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