Array.filter vs lodash filter

JavaScript performance comparison

Revision 3 of this test case created by ES

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.3.1/lodash.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var values = [];
   
    for (var i = 0; i < 100; i++) {
      values.push(i);
    }
   
    function isEven(value) {
      return (value % 2 === 0);
    }
   
    Array.prototype.filter2 = function(
      a, //a function to test each value of the array against. Truthy values will be put into the new array and falsey values will be excluded from the new array
      b, // placeholder
      c, // placeholder
      d, // placeholder
      e // placeholder
    ) {
      c = this; // cache the array
      d = []; // array to hold the new values which match the expression
      for (e in c) // for each value in the array,
      ~~ e + '' == e && e >= 0 && // coerce the array position and if valid,
      a.call(b, c[e], +e, c) && // pass the current value into the expression and if truthy,
      d.push(c[e]); // add it to the new array
   
      return d // give back the new array
    };
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
lodash
_.filter(values, isEven);
pending…
Array.filter
values.filter(isEven);
pending…
filter2
values.filter2(isEven)
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment