string vs array indexOf

Benchmark created by Jens Arps 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. "String success" vs. "array success", then "String fail" vs. "Array fail" and so on.

Preparation HTML

<script>
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.trim().split(" ");

var toFind = "izzle";
var toFail = "uzzle";

// short version

var arrShort = ["foo", "bar", "baz"];
var strShort = " foo bar baz ";
var toFindShort = "bar"
var toFailShort = "boo";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
String success
str.indexOf(" " + toFind + " ");
ready
Array success
arr.indexOf(toFind);
ready
String fail
str.indexOf(" " + toFail + " ");
ready
Array fail
arr.indexOf(toFail);
ready
Short String success
strShort.indexOf(" " + toFindShort + " ");
ready
Short Array success
arrShort.indexOf(toFindShort);
ready
Short String fail
strShort.indexOf(" " + toFailShort + " ");
ready
Short Array fail
arrShort.indexOf(toFailShort);
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