JS startsWith (v5)

Revision 5 of this benchmark created by timmywil on


Description

// Unfortunately, this doesn't work for checking if a string starts with a prefix e.g. 'hehe'.lastIndexOf('he') === 0 // false

// Any of these other options will work

Preparation HTML

<script>
  // sample data
  var origString = 'hello, world',
      testString = 'he';
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
origString.indexOf(testString) === 0;
ready
create regex, then test
var r = new RegExp('^' + testString);
r.test(origString);
ready
lastIndexOf
origString.lastIndexOf(testString) === 0;
ready
substring
origString.substring(0, testString.length) === testString;
ready
substr
origString.substr(0, testString.length) === testString;
ready

Revisions

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