I see that indexOf is faster when you're scanning against one word according to the solution here JavaScript: indexOf vs. Match when Searching Strings?
However, what if you have a list of say 5 keywords and you want to count the occurrence of each (assuming each word appears only once in the string of large text).
Would the below be faster?
var list1 = ['word1', 'word2','word3','word4','word5'];
for (var i = 0; i < list1.length; i++){
if (exampleLargeText.indexOf(list1[i]) > -1){
keywordCounter++;
}
}
vs....
var keywordRegex = 'word1|word2|word3|word4|word5'];
var keywordCounter = exampleLargeText.toLowerCase().match(new RegExp(SUBMIT_ELEMENT_REGEX , "ig")) || []).length
Is indexOf() still faster despite the fact that you're scanning exampleLargeText 5 times here?