_.filter vs Array.filter vs Array.slice (v25)

Revision 25 of this benchmark created by Tom on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>

Setup

var n, N, i, I, sparse = [],
        full = [],
        full2 = [];
    
    N = 16384;
    I = 8;
    
    for (i = 0; i < I; i++) {
      do {
        n = (N * Math.random()) | 0;
        if (n > (N - 1)) {
          n = N - 1;
        }
      } while (sparse[n]);
      sparse[n] = -100 + 200 * Math.random();
    }
    full.length = N;
    full2.length = N;
    for (i = 0; i < N; i++) {
      if (typeof sparse[i] === "number") {
        full[i] = sparse[i];
        full2[i] = sparse[i];
      } else {
        full[i] = 0;
        full2[i] = -100 + 200 * Math.random();
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
_.filter
full = _.filter(full, function(el, index, array) {
  return index == 14;
});
ready
Array.filter
full = full.filter(function(el, index, array) {
  return index == 14;
});
ready
Array.slice
full = full.slice(14).concat(full.slice(15, full.length));
ready

Revisions

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