string vs array indexOf vs regexp test (v3)

Revision 3 of this benchmark created on


Description

This test compares to common alternatives when trying to find out if a given item is part of a list.

To read the results, compare each "pair" of tests, i.e. "Array success" vs. "RegExp success" vs. "String success", then "Array fail", then "RegExp fail" vs. "String fail" and so on.

Setup

var str = "Lorizzle go to hizzle ass sit fo shizzle, sure adipiscing break yo neck, yall. Nullam sapien shiznit, we gonna chung volutpizzle, suscipit pimpin', gravida boom shackalack, arcu. The bizzle pizzle black. Boofron erizzle. For sure izzle brizzle dapibizzle that's the shizzle tempus fo shizzle my nizzle. Maurizzle dope nibh izzle hizzle. Fizzle izzle you son of a bizzle. Pellentesque shit daahng dawg nisi. In boofron platea dictumst. Donec dapibizzle. Fo shiznit, pretizzle funky fresh, mattizzle ac, eleifend vitae, nunc. I saw beyonces tizzles and my pizzle went crizzle suscipit. Integizzle owned velizzle sed purus.";
    var arr = str.split(" ");
    var arrShort = ["foo", "bar", "baz"];
    var strShort = "foo bar baz";
    
    var toFind = "izzle";
    var toFail = "uzzle";
    
    var toFindShort = "bar"
    var toFailShort = "boo";
    
    var reFind = /\bizzle\b/;
    var reFail = /\buzzle\b/;
    var reFindShort = /(?:^| )bar(?: |$)/;
    var reFailShort = /(?:^| )boo(?: |$)/;

Test runner

Ready to run.

Testing in
TestOps/sec
Array success
arr.indexOf(toFind) > -1;
ready
RegExp success
reFind.test(str);
ready
String success
(" " + str + " ").indexOf(" " + toFind + " ") > -1;
ready
Array fail
arr.indexOf(toFail) > -1;
ready
RegExp fail
reFail.test(str);
ready
String fail
(" " + str + " ").indexOf(" " + toFail + " ") > -1;
ready
Short Array success
arrShort.indexOf(toFindShort) > -1;
ready
Short RegExp success
reFindShort.test(strShort);
ready
Short String success
(" " + strShort + " ").indexOf(" " + toFindShort + " ") > -1;
ready
Short Array fail
arrShort.indexOf(toFailShort) > -1;
ready
Short RegExp fail
reFailShort.test(strShort);
ready
Short String fail
(" " + strShort + " ").indexOf(" " + toFailShort + " ") > -1;
ready

Revisions

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

  • Revision 1: published by Jens Arps on
  • Revision 2: published by John-David Dalton on
  • Revision 3: published on