Compact-Map vs Reduce (v2)

Revision 2 of this 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 push
list.reduce((acc, item) => {
    if (item.present) {
    	acc.push({id: item.id})
    }
    return acc;
}, []);
ready
Reduce copy array
list.reduce((acc, item) => {
    if (!item.present) return acc;
    return [...acc, {id: item.id}];
}, []);
ready

Revisions

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