$.inArray, _.indexOf, lodash.indexOf, a simple loop, and Array#indexOf

JavaScript performance comparison

Revision 18 of this test case created by Andy

Info

Added modified loop test to check in half loop

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js">
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js">
</script>
<script>
  var lodash = _.noConflict();
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.3/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var r, $ = window.$,
        _ = window._,
        lodash = window.lodash;
   
    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, 940, 425, 701, 814, 323, 33, 800, 930, 103, 308, 851, 356, 76, 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, 80, 553, 987, 361, 46, 349, 573, 848, 515, 123, 484, 166, 485, 133, 387, 616, 31, 768, 957, 246, 693, 410, 935, 269, 325, 359, 120, 871, 309, 405, 338, 346, 35, 589, 403, 276, 44, 15, 703, 967, 98, 8, 471, 402, 274, 415, 401, 929, 828, 702, 19, 247, 698, 105, 705, 122, 855, 748, 214, 397, 621, 97, 964, 288, 242, 942, 491, 714, 724, 627, 962, 448, 599, 900, 516, 253, 750, 511, 576, 943, 187, 733, 736, 571, 989, 569, 946, 683, 656, 169, 107, 490, 136, 668, 354, 483, 754, 611, 609, 524, 872, 986, 858, 462, 231, 679, 561, 178, 49, 953, 126, 760, 168, 577, 127, 769, 915, 867, 546, 270, 922, 923, 651, 740, 348, 337, 558, 79, 421, 414, 983, 234, 859, 995, 945, 786, 566, 445, 932, 897, 229, 505, 277, 709, 486, 335, 106, 91, 806, 528, 970, 36, 72, 808, 579, 715, 653, 227, 173, 842, 326, 513, 597, 862, 311, 637, 785, 839, 367, 381, 918, 605, 345, 634, 391, 189, 469, 409, 73, 630, 570, 978, 614, 358, 446, 423, 295, 362, 407, 248, 889, 898, 790, 916, 924, 654, 907, 55, 266, 826, 428, 235, 406, 592, 287, 704, 441, 218, 545, 408, 129, 329, 453, 492, 500, 753, 992, 286, 543, 993, 379, 478, 834, 351, 252, 84, 115, 713, 330, 663, 509, 294, 675, 532, 711, 720, 217, 27, 526, 429, 144, 58, 908, 652, 980, 12, 459, 422, 48, 542, 665, 137, 241, 706, 479, 480, 552, 795, 154, 581, 909, 292, 954, 811, 788, 426, 130, 179, 742, 746, 874, 773, 161, 761, 256, 482, 658, 343, 948, 431, 731, 400, 787, 451, 199, 525, 77, 430, 971, 969, 729, 210, 671, 666, 95, 458, 933, 882, 813, 108, 587, 417, 885, 869, 931, 223, 580, 635, 998, 255, 404, 293, 707, 9, 470, 994, 452, 283, 819, 215, 435, 879, 11, 370, 507, 802, 61, 956, 439, 416, 725, 959, 117, 474, 921, 186, 793, 147, 805, 617, 673, 220, 298, 700, 238, 5, 562, 164, 344, 979, 82, 300, 193, 556, 682, 57, 368, 771, 208, 190, 644, 443, 87, 225, 304, 245, 59, 809, 717, 984, 369, 650, 643, 373, 265, 393, 204, 949, 638, 160, 999, 104, 209, 195, 677, 350, 659, 333, 388, 824, 628, 539, 847, 392, 536, 496, 141, 619];
   
    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 = -1 + array.length, m=Math.floor((l+1)/2); i <= m; i++) {
        if (array[i] === value) return true;
        else if (array[(l-i)] === value) return true;    
      }
      return false;
    }
};
</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
$. inArray
r = $.inArray(87, array) > -1;
pending…
_.indexOf
r = _.indexOf(array, 87) > -1;
pending…
Simple loop
r = contains(array, 87);
pending…
Array#indexOf
r = array.indexOf(87) > -1;
pending…
lodash.indexOf
r = lodash.indexOf(array, 87) > -1;
pending…
lodash.contains
r = lodash.contains(array, 87);
pending…
Simpler loop
r = contains2(array, 87);
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