compactmap-vs-filter-boolean

Benchmark created on


Setup

const testVal = {}

for(let x=0; x<200; x++) {
	testVal[`test{$x}`] = {child:!!x};
}

Test runner

Ready to run.

Testing in
TestOps/sec
CompactMap
function compactMap(
  array,
  mapFn,
){
  const length = array.length;
  const result = [];

  let resIndex = 0;

  for (let index = 0; index < length; ++index) {
    const value = mapFn(array[index], index);

    if (value != null) {
      result[resIndex++] = value;
    }
  }
  return result;
}

ready
Filter Map
Object.values(testVal)
      .map(testVal => testVal.child)
      .filter(Boolean)
ready

Revisions

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