filter vs scope

Benchmark created by jasonpvp on


Preparation HTML

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

Setup

var list = [];
    for (var i = 0; i < 3000; i++) {
      list.push({id: i, enabled: (i%2 == 0)});
    }
    
    function enabled() {
      return _.filter(list, function(item) { return item.enabled });
    }
    
    function scope(condition) {
      return {
       forEach: function(callback) {
         var fn = function(item) {
          if (condition(item)) { callback(item) };
         };
         _.forEach(list, fn);
       }
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
filter
var selection = enabled(),
    sum = 0;
_.forEach(selection, function(item) {sum += item.id});
ready
scope
var selection = scope(function(item) { return item.enabled;}),
    sum = 0;
selection.forEach(function(item) {sum += item.id});
ready

Revisions

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

  • Revision 1: published by jasonpvp on