Koukia

Vancouver based Software Engineer and Leader, Geek, Hiker …. Living my dream life.

Follow publication

Member-only story

Top 6 ways to search for a string in JavaScript — And performance benchmarks

Aram Koukia
Koukia
Published in
2 min readJun 18, 2017

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:

Published in Koukia

Vancouver based Software Engineer and Leader, Geek, Hiker …. Living my dream life.

Written by Aram Koukia

Software Engineer, Engineering Leader and Manager @ AWS. Living my dream life. http://koukia.ca/

Responses (3)

Write a response