Array.filter vs lodash.filter without bias (v4)

Revision 4 of this benchmark created on


Description

Test performance of native filter method for arrays vs lodash filter method

Preparation HTML

<script src="//cdn.rawgit.com/lodash/lodash/54644cda79c7074ff710c73ecab7a8a91d583414/dist/lodash.js"></script>

Setup

var List = function (value) {
      this.value = value || [];
    };
     
    List.prototype.unit = function (value) {
      return new List(value);
    };
     
    List.prototype.flatMap = function (fn, context) {
      var results = [];
      this.value.forEach(function (value, key, map) {
        var ret = fn.call(context, value, key, map);
        if (ret instanceof List) {
          ret = ret.get();
        }
        if (Array.isArray(ret)) {
          [].push.apply(results, ret);
        }
        else if (ret) {
          results.push(ret);
        }
      });
      return new List(results);
    };
     
    List.prototype.get = function () {
      return this.value;
    };
     
    List.prototype.map = function (fn) {
      return this.flatMap(function (x) {
        return this.unit(fn(x));
      }, this);
    };
     
    List.prototype.filter = function (fn) {
      return this.flatMap(function (x) {
        return fn(x) ? this.unit(x) : null;
      }, this);
    };
     
    List.prototype.filterNot = function (fn) {
      return this.flatMap(function (x) {
        return fn(x) ? null : this.unit(x);
      }, this);
    };
    
    var a1 = _.range(100);
    
    var a2 = _.union.apply(_, _.times(34, function() {
      return [
        { name: 'john', age: 47 },
        { name: 'jane', age: 22 },
        { name: 'bill', age: 60 }
      ];
    }));

Test runner

Ready to run.

Testing in
TestOps/sec
lodash filter
var r1 = _.filter(a1, function (n) { return n % 2 === 0; });

var r2 = _.filter(a2, function (person) { return person.age > 50; });
ready
Array.filter
var r1 = a1.filter(function (n) { return n % 2 === 0; });

var r2 = a2.filter(function (person) { return person.age > 50; });
ready

Revisions

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