_.filter vs _.where

JavaScript performance comparison

Revision 2 of this test case created

Info

Test underscore and lodash filter vs where performance. A for loop is also tested for better comparison.

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
  var lodash = _.noConflict();
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script>
  var underscore = _.noConflict();
</script>

<script>
var data = {"_id":{"$id":"50311e6fb99e7be45a86a19e"},"awayScore":"2","awayScoreFT":"2","awayScoreHT":"2","enetStageId":827652,"homeScore":"1","homeScoreFT":"1","homeScoreHT":"0","incidents":[{"code":{"id":"g","category":"g"},"player":{"name":"Papiss Cisse"},"side":"away","elapsed":"19","sortorder":3},{"code":{"id":"a","category":"a"},"player":{"name":"Moussa Sissoko"},"side":"away","elapsed":"19","sortorder":3},{"side":"away","elapsed":"31","sortorder":4,"_id":{"$id":"51082e2e0c7dfc9979000d9c"},"player":{"name":"Yohan Cabaye"},"code":{"id":"g","category":"g"}},{"side":"home","elapsed":"42","sortorder":0,"_id":{"$id":"510830c20c7dfc9979000da0"},"player":{"name":"Charles N\u00b4Zogbia"},"code":{"id":"y","category":"c"}},{"code":{"id":"so","category":"s"},"player":{"name":"Darren Bent"},"side":"home","elapsed":"46","sortorder":0},{"code":{"id":"si","category":"s"},"player":{"name":"Gabriel Agbonlahor"},"side":"home","elapsed":"46","sortorder":0},{"side":"home","elapsed":"46","sortorder":0,"_id":{"$id":"510835e90c7dfc9979000da7"},"player":{"name":"Joseph Bennett"},"code":{"id":"so","category":"s"}},{"side":"home","elapsed":"46","sortorder":0,"_id":{"$id":"510835e90c7dfc9979000da8"},"player":{"name":"Andreas Weimann"},"code":{"id":"si","category":"s"}},{"side":"home","elapsed":"49","sortorder":0,"_id":{"$id":"510836610c7dfc9979000da9"},"player":{"name":"Christian Benteke"},"code":{"id":"p","category":"g"}},{"code":{"id":"y","category":"c"},"player":{"name":"Jonas Guti\u00e9rrez"},"side":"away","elapsed":"52","sortorder":0},{"side":"home","elapsed":"61","sortorder":1,"_id":{"$id":"510839a90c7dfc9979000db7"},"player":{"name":"Christian Benteke"},"code":{"id":"y","category":"c"}},{"side":"home","elapsed":"67","sortorder":0,"_id":{"$id":"51083a990c7dfc9979000dbf"},"player":{"name":"Nathan Baker"},"code":{"id":"y","category":"c"}},{"side":"away","elapsed":"69","sortorder":0,"_id":{"$id":"51083b110c7dfc9979000dc0"},"player":{"name":"James Perch"},"code":{"id":"so","category":"s"}},{"side":"away","elapsed":"69","sortorder":0,"_id":{"$id":"51083b110c7dfc9979000dc1"},"player":{"name":"Shola Ameobi"},"code":{"id":"si","category":"s"}},{"side":"away","elapsed":"74","sortorder":0,"_id":{"$id":"51083c3d0c7dfc9979000dcc"},"player":{"name":"Yoan Gouffran"},"code":{"id":"so","category":"s"}},{"side":"away","elapsed":"74","sortorder":0,"_id":{"$id":"51083c3d0c7dfc9979000dcd"},"player":{"name":"Vurnon Anita"},"code":{"id":"si","category":"s"}},{"side":"home","elapsed":"76","sortorder":0,"_id":{"$id":"51083cb50c7dfc9979000dce"},"player":{"name":"Matthew Lowton"},"code":{"id":"y","category":"c"}},{"side":"away","elapsed":"80","sortorder":0,"_id":{"$id":"51083d690c7dfc9979000dcf"},"player":{"name":"Moussa Sissoko"},"code":{"id":"so","category":"s"}},{"side":"away","elapsed":"80","sortorder":0,"_id":{"$id":"51083d690c7dfc9979000dd0"},"player":{"name":"Mapou Yanga-Mbiwa"},"code":{"id":"si","category":"s"}},{"side":"home","elapsed":"78","sortorder":2,"_id":{"$id":"51083da50c7dfc9979000dd1"},"player":{"name":"Gabriel Agbonlahor"},"code":{"id":"y","category":"c"}},{"side":"away","elapsed":"87","sortorder":3,"_id":{"$id":"51083f490c7dfc9979000dda"},"player":{"name":"Shola Ameobi"},"code":{"id":"y","category":"c"}}],"league":"epl","lineup":[{"side":"home","type":"Coach","_id":{"$id":"51e42533472b17170e01f96a"},"participant":{"name":"Paul Lambert"}},{"side":"away","type":"Coach","_id":{"$id":"51e42533472b17170e01f96b"},"participant":{"name":"Alan Pardew"}},{"side":"home","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087bd"},"participant":{"name":"Marc Albrighton"}},{"side":"home","type":"Doubtful","_id":{"$id":"51e7ddd4f092c8c80d0087be"},"participant":{"name":"Richard Dunne"}},{"side":"home","type":"Unavailable","_id":{"$id":"51e7ddd4f092c8c80d0087bf"},"participant":{"name":"Karim El Ahmadi"}},{"side":"home","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c0"},"participant":{"name":"Gary Gardner"}},{"side":"home","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c1"},"participant":{"name":"Chris Herd"}},{"side":"home","type":"Unavailable","_id":{"$id":"51e7ddd4f092c8c80d0087c2"},"participant":{"name":"Stiliyan Petrov"}},{"side":"away","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c3"},"participant":{"name":"Hatem Ben Arfa"}},{"side":"away","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c4"},"participant":{"name":"Dan Gosling"}},{"side":"away","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c5"},"participant":{"name":"Ryan Taylor"}},{"side":"away","type":"Injured","_id":{"$id":"51e7ddd4f092c8c80d0087c6"},"participant":{"name":"Danny Simpson"}},{"side":"away","type":"Unavailable","_id":{"$id":"51e7ddd4f092c8c80d0087c7"},"participant":{"name":"Cheik Tiot\u00e9"}},{"side":"away","type":"Injured","_id":{"$id":"51e7ddd5f092c8c80d0087c8"},"participant":{"name":"Haris Vuckic"}},{"side":"away","type":"Starter","positionName":"RB","enetPos":32,"_id":{"$id":"51e7ddd5f092c8c80d0087c9"},"participant":{"name":"Mathieu Debuchy"}},{"side":"away","type":"Starter","positionName":"GK","enetPos":11,"_id":{"$id":"51e7ddd5f092c8c80d0087ca"},"participant":{"name":"Tim Krul"}},{"side":"away","type":"Starter","positionName":"LB","enetPos":38,"_id":{"$id":"51e7ddd5f092c8c80d0087cb"},"participant":{"name":"Davide Santon"}},{"side":"home","type":"Starter","positionName":"GK","enetPos":11,"_id":{"$id":"51e7ddd5f092c8c80d0087cc"},"participant":{"name":"Brad Guzan"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008875"},"participant":{"name":"Mapou Yanga-Mbiwa"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008876"},"participant":{"name":"Shay Given"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008877"},"participant":{"name":"Jordan Bowery"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008878"},"participant":{"name":"Vurnon Anita"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008879"},"participant":{"name":"Sammy Ameobi"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d00887a"},"participant":{"name":"Brett Holman"}},{"side":"home","type":"Doubtful","_id":{"$id":"51e7dde6f092c8c80d00887b"},"participant":{"name":"Fabian Delph"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d00887c"},"participant":{"name":"Mike Williamson"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d00887d"},"participant":{"name":"Shola Ameobi"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d00887e"},"participant":{"name":"Gabriel Agbonlahor"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d00887f"},"participant":{"name":"Eric Lichaj"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008880"},"participant":{"name":"Steve Harper"}},{"side":"away","type":"Substitute","_id":{"$id":"51e7dde6f092c8c80d008881"},"participant":{"name":"Gael Bigirimana"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde7f092c8c80d008882"},"participant":{"name":"Stephen Ireland"}},{"side":"home","type":"Substitute","_id":{"$id":"51e7dde7f092c8c80d008883"},"participant":{"name":"Andreas Weimann"}},{"side":"home","type":"Starter","positionName":"RB","enetPos":33,"_id":{"$id":"51e7dde8f092c8c80d00889d"},"participant":{"name":"Ciaran Clark"}},{"side":"home","type":"Starter","positionName":"RM","enetPos":73,"_id":{"$id":"51e7dde8f092c8c80d00889e"},"participant":{"name":"Ashley R. Westwood"}},{"side":"home","type":"Starter","positionName":"CF","enetPos":106,"_id":{"$id":"51e7dde8f092c8c80d00889f"},"participant":{"name":"Christian Benteke"}},{"side":"home","type":"Starter","positionName":"RB","enetPos":31,"_id":{"$id":"51e7dde9f092c8c80d0088a1"},"participant":{"name":"Matthew Lowton"}},{"side":"home","type":"Starter","positionName":"LB","enetPos":39,"_id":{"$id":"51e7dde9f092c8c80d0088a2"},"participant":{"name":"Joseph Bennett"}},{"side":"home","type":"Starter","positionName":"CF","enetPos":104,"_id":{"$id":"51e7dde9f092c8c80d0088a3"},"participant":{"name":"Darren Bent"}},{"side":"home","type":"Starter","positionName":"LB","enetPos":37,"_id":{"$id":"51e7dde9f092c8c80d0088a7"},"participant":{"name":"Nathan Baker"}},{"side":"home","type":"Starter","positionName":"LM","enetPos":77,"_id":{"$id":"51e7dde9f092c8c80d0088a8"},"participant":{"name":"Charles N\u00b4Zogbia"}},{"side":"home","type":"Starter","positionName":"CB","enetPos":35,"_id":{"$id":"51e7dde9f092c8c80d0088aa"},"participant":{"name":"Ron Vlaar"}},{"side":"home","type":"Starter","positionName":"CM","enetPos":75,"_id":{"$id":"51e7dde9f092c8c80d0088ab"},"participant":{"name":"Barry Bannan"}},{"side":"away","type":"Starter","positionName":"CB","enetPos":36,"_id":{"$id":"51e7dde9f092c8c80d0088ac"},"participant":{"name":"Steven Taylor"}},{"side":"away","type":"Starter","positionName":"CB","enetPos":34,"_id":{"$id":"51e7dde9f092c8c80d0088ad"},"participant":{"name":"Fabricio Coloccini"}},{"side":"away","type":"Starter","positionName":"DM","enetPos":64,"_id":{"$id":"51e7ddeaf092c8c80d0088b0"},"participant":{"name":"James Perch"}},{"side":"away","type":"Starter","positionName":"LM","enetPos":87,"_id":{"$id":"51e7ddeaf092c8c80d0088b1"},"participant":{"name":"Yoan Gouffran"}},{"side":"away","type":"Starter","positionName":"CM","enetPos":85,"_id":{"$id":"51e7ddeaf092c8c80d0088b2"},"participant":{"name":"Moussa Sissoko"}},{"side":"away","type":"Starter","positionName":"RM","enetPos":83,"_id":{"$id":"51e7ddeaf092c8c80d0088b3"},"participant":{"name":"Jonas Guti\u00e9rrez"}},{"side":"away","type":"Starter","positionName":"DM","enetPos":66,"_id":{"$id":"51e7ddeaf092c8c80d0088b4"},"participant":{"name":"Yohan Cabaye"}},{"side":"away","type":"Starter","positionName":"ST","enetPos":115,"_id":{"$id":"51e7ddeaf092c8c80d0088b5"},"participant":{"name":"Papiss Cisse"}}],"matchID":1345461,"refereeName":"Mike Dean","region":"England","round":"24","season":"2012\/2013","spectators":30334,"stage":"Premier League","venueName":"Villa Park"}
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
underscore.filter
var result = underscore.filter(data.lineup, function(lineup) {
  if (lineup.type == 'Starter') {
          return true;
  }
});
pending…
underscore.where
var result = underscore.where(data.lineup, {type: 'Starter'});
pending…
for loop
var result = (function() {
  var players = [];
  for (var i = 0; i < data.lineup.length; i++) {
          if (data.lineup[i].type == 'Starter') {
                  players.push(data.lineup[i]);
          }
        }
  return players;
}());
pending…
lodash.filter
var result = lodash.filter(data.lineup, function(lineup) {
  if (lineup.type == 'Starter') {
          return true;
  }
});
pending…
lodash.where
var result = lodash.where(data.lineup, {type: 'Starter'});
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