LoDash, Underscore, and Native contains (v2)

Revision 2 of this benchmark created on


Description

A quick look at Lo-Dash 2.4.1, Underscore 1.7, and Native Performance on common functional operations.

This is for contains

Preparation HTML

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

Setup

var arr = [];
    var arr2 = [];
    for (var i = 0; i < 10000; i++) {
      arr.push({
        index: i,
        random: Math.random() * 10000
      });
    }
    
    var testItem = arr[200];
    var notInArray = { not:'inarray'};

Test runner

Ready to run.

Testing in
TestOps/sec
Native contains (indexOf)
var shouldBeTrue = arr.indexOf(testItem) >= 0;
var shouldBeFalse = arr.indexOf(notInArray) >= 0;
ready
Underscore contains
var shouldBeTrue = underscore.contains(arr,testItem);
var shouldBeFalse = underscore.contains(arr, notInArray);
ready
Lodash Contains
var shouldBeTrue = lodash.contains(arr,testItem);
var shouldBeFalse = lodash.contains(arr, notInArray);
ready
Native Loop Contains
var i = 0, l = arr.length,shouldBeTrue = false, shouldBeFalse = false;

for(;i<l; i++) {
 if(arr[i] === testItem) {
   shouldBeTrue = true; 
   break;
 }
}
i=0;
for(;i<l; i++) {
 if(arr[i] === notInArray) {
   shouldBeFalse = true; 
   break;
 }
}
ready

Revisions

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

  • Revision 1: published by Ben McCormick on
  • Revision 2: published on