Compact-Map vs Reduce

Benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js"></script> 

Setup

var list = [{id: 1, present: true}, {id: 2, present: false}, {id: 3, present: true}];

Test runner

Ready to run.

Testing in
TestOps/sec
Compact-map
_.compact(
    list.map((item) => {
      if (!item.present) return undefined;
      return {id: item.id};
    })
  );
ready
Reduce
list.reduce((acc, item) => {
    if (item.present) {
    	acc.push({id: item.id})
    }
    return acc;
}, []);
ready

Revisions

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