lodash: map+compact vs filter+map

Benchmark created by Jean 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
compact & map
_.compact(_.map(a, function(item){
  if(item.even) {
    item.pow = item.i * item.i;
    return item;
  }
}));
ready
filter & map
_.filter(a, function(item){
  return item.even;
}).map(function(item){
  item.pow = item.i * item.i;
  return item;
});
ready

Revisions

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