string find

Benchmark created on


Setup

// Source - https://stackoverflow.com/a/6321934
// Posted by T.J. Crowder, modified by community. See post 'Timeline' for change history
// Retrieved 2025-11-07, License - CC BY-SA 3.0

// ==== Main Setup
var toFind = ["aaaaa100@zzzzz", "aaaaa200@zzzzz", "aaaaa300@zzzzz", "aaaaa400@zzzzz", "aaaaa500@zzzzz"];
var theString = (function() {
 var m, n;

 m = [];
 for (n = 1; n <= 500; ++n) {
  m.push("aaaaa" + n + "@zzzzz");
 }
 return m.join(" ");
})();

// ==== String#indexOf (and RegExp) setup for when we can ignore setup
var preppedString = " " + theString + " ";

// ==== RegExp setup for test case ignoring RegExp setup time
var theRegExp = new RegExp(" (?:" + toFind.join("|") + ") ", "g");

// ==== Dictionary setup for test case ignoring Dictionary setup time
var theDictionary = (function() {
 var dict = {};
 var index;
 var values = theString.split(" ");
 for (index = 0; index < values.length; ++index) {
  dict[values[index]] = true;
 }
 return dict;
})();

// ==== Array setup time for test cases where we ignore array setup time
var theArray = theString.split(" ");

Test runner

Ready to run.

Testing in
TestOps/sec
indexof
// Source - https://stackoverflow.com/a/6321934
// Posted by T.J. Crowder, modified by community. See post 'Timeline' for change history
// Retrieved 2025-11-07, License - CC BY-SA 3.0

var index;
for (index = 0; index < toFind.length; ++index) {
 if (preppedString.indexOf(toFind[index]) < 0) {
  throw "Error";
 }
}
ready
re alt
// Source - https://stackoverflow.com/a/6321934
// Posted by T.J. Crowder, modified by community. See post 'Timeline' for change history
// Retrieved 2025-11-07, License - CC BY-SA 3.0

// Note: In real life, you'd have to escape the values from toFind
// to make sure they didn't have special regexp chars in them
var regexp = new RegExp(" (?:" + toFind.join("|") + ") ", "g");
var match, counter = 0;
var str = " " + theString + " ";
for (match = regexp.exec(str); match; match = regexp.exec(str)) {
 ++counter;
}
if (counter != 5) {
 throw "Error";
}
ready

Revisions

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