JavaScript startsWith() Alternative Methods Performance

Benchmark created on


Setup

const inputString = 'Hello, JavaScript!'
const searchText = 'Hello'

Test runner

Ready to run.

Testing in
TestOps/sec
Using startsWith() method
console.log(inputString.startsWith(searchText))
ready
Using indexOf() alternate method
if (inputString.indexOf(searchText) === 0) {
  console.log('String starts with "Hello".')
}
ready
Using lastIndexOf() alternate method
if (inputString.lastIndexOf(searchText, 0) === 0) {
  console.log('String starts with "Hello".')
}
ready
Using match() alternate method
if (inputString.match(searchText) && inputString.match(searchText).length > 0) {
  console.log('String starts with "Hello".')
}
ready
Using slice() alternate method
if (inputString.slice(0, searchText.length) === searchText) {
  console.log(`String starts with "${searchText}"`)
}
ready
Using substring() alternate method
if (inputString.substring(0, searchText.length) === searchText) {
  console.log(`String starts with "${searchText}"`)
}
ready

Revisions

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