I'm working with a performance issue on JavaScript. So I just want to ask: what is the fastest way to check whether a string contains another substring (I just need the boolean value)? Could you please suggest your idea and sample snippet code?
10 Answers
You have three possibilites:
-
(new RegExp('word')).test(str) // or /word/.test(str) -
str.indexOf('word') !== -1 -
str.includes('word')
Regular expressions seem to be faster (at least in Chrome 10).
Performance test - short haystack
Performance test - long haystack
**Update 2011:**
It cannot be said with certainty which method is faster. The differences between the browsers is enormous. While in Chrome 10 indexOf seems to be faster, in Safari 5, indexOf is clearly slower than any other method.
You have to see and try for your self. It depends on your needs. For example a case-insensitive search is way faster with regular expressions.
Update 2018:
Just to save people from running the tests themselves, here are the current results for most common browsers, the percentages indicate performance increase over the next fastest result (which varies between browsers):
Chrome: indexOf (~98% faster) <-- wow
Firefox: cached RegExp (~18% faster)
IE11: cached RegExp(~10% faster)
Edge: indexOf (~18% faster)
Safari: cached RegExp(~0.4% faster)
Note that cached RegExp is: var r = new RegExp('simple'); var c = r.test(str); as opposed to: /simple/.test(str)
16 Comments
indexOf doesn't work. I'm not sure why. Using Regex though does. This is an edge case, but others might run into the same issue.The Fastest
- (ES6) includes
var string = "hello",
substring = "lo";
string.includes(substring);
- ES5 and older indexOf
var string = "hello",
substring = "lo";
string.indexOf(substring) !== -1;
Comments
Does this work for you?
string1.indexOf(string2) >= 0
Edit: This may not be faster than a RegExp if the string2 contains repeated patterns. On some browsers, indexOf may be much slower than RegExp. See comments.
Edit 2: RegExp may be faster than indexOf when the strings are very long and/or contain repeated patterns. See comments and @Felix's answer.
6 Comments
test.indexOf is a magnitude slower than any other method. So it actually cannot be said which method is faster. It varies from browser to browser.In ES6, the includes() method is used to determine whether one string may be found within another string, returning true or false as appropriate.
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
Here is jsperf between
var ret = str.includes('one');
And
var ret = (str.indexOf('one') !== -1);
As the result shown in jsperf, it seems both of them perform well.
3 Comments
str.includes("x|y"); search for the literals "x" or "y" in the same call.regex in it. One work around for your question, str.includes("x") || str.includes('y')indexOf is significantly faster than includes (upwards of 1600% faster). It's unclear how a difference of 44 million iterations/sec and 777+ million i/sec affects real-world performance, however mobile likely benefits enough that indexOf should be the ideal choice.I've found that using a simple for loop, iterating over all elements in the string and comparing using charAt performs faster than indexOf or Regex. The code and proof is available at JSPerf.
ETA: indexOf and charAt both perform similarly terrible on Chrome Mobile according to Browser Scope data listed on jsperf.com
5 Comments
It's easy way to use .match() method to string.
var re = /(AND|OR|MAYBE)/;
var str = "IT'S MAYBE BETTER WAY TO USE .MATCH() METHOD TO STRING";
console.log('Do we found something?', Boolean(str.match(re)));
Wish you a nice day, sir!
1 Comment
match when there's a test method… Check out the top answer.For finding a simple string, using the indexOf() method and using regex is pretty much the same: http://jsperf.com/substring - so choose which ever one that seems easier to write.
Comments
I made a jsben.ch for you http://jsben.ch/#/aWxtF ...seems that indexOf is a bit faster.
2022 string research benchmark
From Felix Kling's answer, and with tests I've done with given links.
Most used browser :
- Chrome (64%)
- Safari (19%)
- New Edge (4%)
- Firefox (3.26%)
- Samsung (2.86%)
- Opera (2.12%)
Chrome and NE are both based on Chromium => same performances.
ci = case insensitive
/ = same as left
Tests results
| string length | Firefox | Safari | Chromium | |
|---|---|---|---|---|
| short | cached RegExp ci | cached RegExp ci | indexOf & / ci | worth |
| RegExp & / ci | RegExp ci | RegExp & / ci | worse | |
| long | cached RegExp | cached RE & / ci & reg ci | indexOf | worth |
| indexOf ci | indexOf ci | RegExp | worse |
Operations/sec comparison
| browser | Firefox | Safari | Chromium |
|---|---|---|---|
| cached RegExp | 1.3M | 425k | 1.2M |
| diff | 1.08x > | ||
| cached RegExp case sensitive | 28M | 31M | 42M |
| diff | 1.44/1.35x > | ||
| indexOf | 27M | 25M | 1.9B |
| diff | 70/76x > | ||
| indexOf case sensitive | 13.8M | 18.5M | 1.9B |
| diff | 137/103x > |
Firefox best method : cached regexp case insensitive
Chrome best method : indexOf / indexOf case insensitive
Safari best method : cached RegExp case insensitive
Chrome has widely better performances than the two others.
Best compromise : indexOf : String.indexOf(substring) > -1.
Note : Remind that if you want to use the indexOf case sensitive way, if you operate a String.toLowerCase(), it adds some operations, so it's pretty similar to the insensitive way. In that case, you should be lowering your substring before the search process, not in it.
Regexes are really good for complex and/or pattern research/replacement, but not for a global research, and that in all languages, because of what it is.
2 Comments
includes testsincludes method is similar to the indexOf, works the same way and has similar performances, because it uses indexOf underneath.According to this site, include is much faster https://www.measurethat.net/Benchmarks/Show/13675/0/regextest-vs-stringincludes-vs-stringmatch

regextag)?