$.inArray, _.indexOf, and a simple loop

JavaScript performance comparison

Revision 28 of this test case created by DocKeR

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js">
</script>
<script src="//cdn.jsdelivr.net/lodash/2.4.1/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script>
Benchmark.prototype.setup = function() {
    var r, $ = window.$,
        _ = window._,
        lodash = window.lodash;
   
    function contains(array, value) {
      var index = -1,
          length = array.length;
   
      while (++index < length) {
        if (array[index] === value) {
          return true;
        }
      }
      return false;
    }
   
    function contains2(array, value) {
      for (var i = 0, m = array.length; i < m; i++) {
        if (array[i] === value) {
          return true;
        }
      }
      return false;
    }
   
    function contains3(array, value) {
      for (var i = 0, l = array.length - 1, m=Math.floor(l/2); i <= m; i++) {
        if (array[i] === value) return true;
        else if (array[(l-i)] === value) return true;    
      }
      return false;
    }
   
    function contains4(array, value) {
      var l = array.length - 1;
      var m = Math.floor(l/2);
      for (var i = 0; i <= m; i++) {
        if (array[i] === value) return true;
        else if (array[(l-i)] === value) return true;    
      }
      return false;
    }
   
    function rcontains(array, value) {
      var index = array.length;
   
      while (--index > -1) {
        if (array[index] === value) {
          return true;
        }
      }
      return false;
    }
   
    var array = [321, 182, 976, 413, 382, 697, 996, 629, 233, 412, 211, 424, 965, 925, 560, 386, 807, 140, 952, 904, 833, 586, 139, 466, 226, 687, 119, 363, 784, 24, 792, 352, 376, 375, 606, 508, 716, 114, 291, 237, 544, 891, 518, 440, 395, 449, 841, 699, 284, 936, 818, 840, 162, 93, 355, 584, 249, 29, 476, 502, 669, 51, 67, 501, 857, 254, 622, 615, 504, 757, 837, 78, 477, 444, 138, 836, 533, 155, 181, 365, 565, 624, 598, 529, 197, 530, 506, 531, 488, 853, 759, 639, 25, 88, 723, 170, 131, 854, 463, 128, 939, 297, 595, 905, 63, 222, 696, 399, 196, 888, 514, 997, 728, 968, 540, 20, 251, 116, 880, 64, 917, 564, 626, 157, 883, 607, 982, 690, 797, 433, 575, 708, 667, 299, 312, 156, 568, 815, 894, 734, 877, 521, 390, 101, 180, 498, 820, 620, 341, 718, 206, 678, 90, 16, 38, 289, 419, 549, 75, 803, 150, 816, 259, 191, 374, 146, 145, 866, 919, 863, 911, 18, 747, 988, 184, 934, 832, 499, 745, 41, 418, 789, 23, 54, 296, 574, 272, 657, 366, 902, 313, 347, 111, 436, 201, 353, 68, 692, 216, 856, 94, 447, 830, 719, 777, 660, 340, 81, 305, 47, 604, 503, 30, 96, 850, 588, 468, 21, 45, 203, 878, 601, 324, 710, 991, 198, 633, 493, 142, 600, 100, 712, 779, 860, 3, 681, 148, 143, 194, 43, 744, 228, 778, 582, 585, 380, 741, 812, 522, 92, 770, 796, 559, 722, 489, 694, 167, 332, 899, 884, 240, 14, 213, 873, 69, 941, 684, 320, 845, 636, 647, 420, 257, 163, 829, 290, 685, 244, 810, 646, 475, 438, 950, 798, 192, 121, 672, 132, 83, 26, 318, 937, 315, 50, 981, 960, 398, 603, 892, 411, 113, 609, 940, 425, 701, 814, 323, 338, 800, 930, 103, 308, 851, 356, 768, 239, 465, 125, 159, 110, 763, 377, 799, 135, 680, 224, 551, 258, 765, 875, 893, 37, 62, 846, 267, 523, 972, 261, 852, 327, 60, 221, 185, 457, 331, 230, 124, 973, 53, 303, 613, 487, 876, 977, 481, 938, 689, 260, 364, 89, 22, 165, 649, 268, 456, 825, 65, 394, 427, 752, 360, 396, 538, 236, 42, 578, 674, 783, 691, 743, 591, 99, 727, 906, 271, 567, 695, 990, 794, 310, 212, 102, 464, 821, 473, 590, 39, 467, 910, 357, 432, 372, 641, 838, 870, 555, 70, 307, 625, 912, 958, 756, 109, 640, 975, 903, 517, 688, 961, 887, 537, 281, 843, 927, 314, 85, 610, 751, 618, 947, 175, 176, 896, 151, 849, 336, 371, 835, 6, 512, 642, 384, 963, 207, 758, 262, 134, 780, 328, 112, 730, 40, 844, 583, 200, 831, 791, 868, 454, 118, 749, 28, 202, 510, 774, 472, 951, 7, 823, 275, 926, 232, 985, 596, 264, 205, 563, 608, 279, 342, 527, 762, 188, 655, 174, 152, 648, 1000, 776, 172, 593, 243, 280, 572, 721, 548, 455, 519, 557, 676, 755, 263, 782, 149, 32, 781, 801, 766, 804, 285, 928, 71, 153, 534, 183, 602, 306, 670, 282, 219, 645, 273, 554, 301, 594, 864, 10, 737, 66, 612, 886, 735, 460, 738, 52, 865, 437, 541, 4, 955, 861, 822, 1, 56, 726, 775, 322, 890, 767, 13, 177, 901, 623, 944, 2, 339, 34, 278, 86, 817, 495, 881, 664, 631, 732, 920, 389, 450, 772, 378, 974, 535, 171, 461, 895, 547, 739, 914, 764, 158, 250, 632, 497, 442, 662, 74, 913, 334, 383, 827, 385, 686, 661, 319, 966, 434, 520, 494, 316, 302, 317, 550, 17];
   
};
</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
Simple Loop
r = contains(array, 609);
pending…
Simpler Loop
r = contains2(array, 609);
pending…
Simpler halved
r = contains3(array, 609);
pending…
Simpler Halved2
r = contains4(array, 609);
pending…
lodash.indexOf
r = lodash.indexOf(array, 609) > -1;
pending…
lodash.contains
r = lodash.contains(array, 609);
pending…
Simple Loop Reversed
r = rcontains(array, 609);
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