Member-only story
Top 6 ways to search for a string in JavaScript — And performance benchmarks

Searching for a sub-string in a string, is one of the most basic operations, that we come across every now and then, and what I usually do is, to look into other places in the code and see what others have done and just do the same thing!
Now, I just wanted to bring up some of the most used options that are out there, as well as a performance comparison between them, in case you care about performance, so when it comes to choosing in the future, you know which one is a better choice!
Show me some code
The following are 6 different ways that I have seen a lot when searching for strings:
Performance Benchmark
In case you are wondering which one will perform better if the string is too big, ‘regex.test’ is the fastest, followed by ‘string.search’ from ES6 libraries, and the third place belongs to string.indexof: