underscore.js filter vs native (v7)

Revision 7 of this benchmark created by Kevin Attfield on


Preparation HTML

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

Setup

window.contacts = [];
    
    var contact;
    
    function getRandomInt (min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    
    for (var i=0; i < 5000; i++) {
      contact = {};
      contact.firstName = 'firstName' + getRandomInt(1, 500);
      contact.lastName = 'lastName' + getRandomInt(1, 500);
      contact.city = ["Boulder", "Denver", "Denver", "Golden"][getRandomInt(0, 3)];
      contact.state = "CO";
      contacts.push(contact);
    }
    
    var boulder = window.boulder = window.boulder || {};

Test runner

Ready to run.

Testing in
TestOps/sec
underscore
boulder['underscore'] = _.filter(contacts, function(contact) {
  return contact.city === 'Boulder';
});
ready
native
boulder['native'] = contacts.filter(function(contact) {
  return contact.city === 'Boulder';
});
ready

Revisions

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