Scroll Down
I’ve rounded up and tested few performant Javascript methods for checking if a string contains a substring.
For all examples please consider var string = “Hello World”; and var substring = “World”;
string.indexOf(substring);
_.includes(string, substring);
string.includes(substring);
string.match(new RegExp(substring));
new RegExp(substring).test(string);
For benchmarking I’ve used this JSPERF test setup.