testingdiwq-small-data

JavaScript performance comparison

Test case created by Cory Danielson

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.7.0/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var users = _.map(_.range(100), 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 = [];

};

Benchmark.prototype.teardown = function() {
  result = [];

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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;
      }
   });
});
pending…
solution 1
result = _.flatten(_.map(others, function(item){
  return _.filter(users, item);
}));
pending…
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];
});
pending…
solution 3
result = _.flatten(_.map(others, function(other){return _.where(users, other);}));
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.

0 Comments