testingdiwq-small-data (v2)

Revision 2 of this benchmark created on


Preparation HTML

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

Setup

var users = _.map(_.range(500), function(i) {
      return { user: 'test', age: i };
  });
  var others = [
      { user: 'test', age: 1 },
      { user: 'test', age: 22 },
      { user: 'test', age: 55 },
      { user: 'test', age: 77 },
      { user: 'test', age: 88 }
  ];
  
  var result = [];

Teardown



            result = [];
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
original
_.forEach(users, function (n, key) {
   _.forEach(others, function (n2, key2) {
      if (n.user === n2.user && n.age === n2.age) {
         result.push(n);
         return false;
      }
   });
});
ready
solution 1
result = _.flatten(_.map(others, function(item){
  return _.filter(users, item);
}));
ready
solution 2
// index others by "user + age"
var lookup = _.indexBy(others, function(o) { return o.user + o.age });
// find all users where "user + age" exists in index, one loop, quick lookup. no nested loops
result = _.filter(users, function(u) {
    return lookup[u.user + u.age];
});
ready
solution 3
result = _.flatten(_.map(others, function(other){return _.where(users, other);}));
ready

Revisions

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