Map + Filter vs FlatMap

Benchmark created on


Setup

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

Test runner

Ready to run.

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

Revisions

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