String: includes vs. startsWith vs. indexOf

Benchmark created on


Setup

const strToSearch = "edcba"
const preparedRegex = /^edcba/
const suffix = "hdzjjkhbjfsheuicdzocbsx"
const arr100 = new Array(100).fill(null).map((_, i) => {
	return strToSearch.substring(0, i % 6) + suffix
})

Test runner

Ready to run.

Testing in
TestOps/sec
includes (which COULD trigger false positives compared to the two other methods)
arr100.filter(str => str.includes(strToSearch))
ready
startsWith
arr100.filter(str => str.startsWith(strToSearch))
ready
indexOf
arr100.filter(str => str.indexOf(strToSearch) === 0)
ready
match (preparedRegex)
arr100.filter(str => str.match(preparedRegex))
ready
match (dyn regex)
arr100.filter(str => str.match(new RegExp(`^${strToSearch}`)))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.