Map + Filter vs FlatMap (v2)

Revision 2 of this benchmark created on


Setup

const dataSet = [
  {
    id: 1,
    value: 'value1'
  },
  {
    id: 2,
    value: 'value2'
  },
  {
    id: 3,
    value: 'value3'
  },
  {
    id: 4,
    value: 'value4'
  },
  {
    id: 5,
    value: 'value5'
  },
  {
    id: 6,
    value: 'value6'
  },
  {
    id: 7,
    value: 'value7'
  },
  {
    id: 8,
    value: 'value8'
  },
  {
    id: 9,
    value: 'value9'
  },
  {
    d: 10,
    value: 'value10'
  },
];

Test runner

Ready to run.

Testing in
TestOps/sec
Map + Filter
const testResult = dataSet.map(({ id, value }) =>
        value ? id : undefined,
      )
      .filter((val) => !!val);
ready
FlatMap
const testResult = dataSet.flatMap(
       ({ id, value }) =>
         value ? id : [],
     );
ready

Revisions

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