lodash: map+compact vs filter+map vs reduce (v3)

Revision 3 of this benchmark created on


Preparation HTML

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

Setup

var a = [];

    for(var i = 0; i < 200; i++){
      a.push({
        i: i,
        even: i % 2 === 0
      })
    }

Test runner

Ready to run.

Testing in
TestOps/sec
filter & map
_.map(_.filter(a, function(item) {
  return item.even;
}),function(item) {
  item.pow = item.i * item.i;
  return item;
});
ready
compact & map
_.compact(_.map(a, function(item) {
  if (item.even) {
    item.pow = item.i * item.i;
    return item;
  }
}));
ready
reduce
_.reduce(a, function(acc, cur) {
  if (cur.even) {
    cur.pow = cur.i * cur.i;
    acc.push(cur);
  }
  return acc;
}, []);
ready

Revisions

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