JS startsWith

JavaScript performance comparison

Revision 7 of this test case created

Info

// 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 code

<script>
  // sample data
  var origString = 'hello, world',
      success = 'he';
 
  var longString = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam imperdiet feugiat bibendum. In in mi odio. Donec sed urna massa, sed fermentum risus. Proin non erat orci. Ut at mauris eget diam mollis tristique. Praesent ornare, dolor ut consectetur tempor, diam dolor luctus metus, eget pharetra mauris nulla id mauris. Phasellus id arcu ut ipsum pretium vulputate sit amet nec metus. Ut lacinia ante dignissim mauris vehicula id placerat diam ornare. Quisque ut eros eget lectus suscipit blandit quis nec ligula. Nulla hendrerit pulvinar leo, sit amet dictum mauris fermentum et. Curabitur vitae consequat purus. Phasellus odio orci, viverra a interdum sit amet, placerat sit amet augue. Vestibulum justo orci, feugiat vitae imperdiet non, molestie ac ante. Praesent in enim est, eget placerat tellus. Suspendisse augue nibh, placerat non pellentesque id, pellentesque sit amet nibh. Mauris ut mi non libero consectetur fermentum. Nunc gravida lectus libero. Pellentesque vel eros et diam fermentum tristique. Proin accumsan sagittis sagittis. Duis elementum velit lectus. Cras mollis enim nec orci iaculis in consequat augue rutrum. Aenean at consectetur arcu. Fusce consequat mi fermentum velit congue consequat ac a elit. Phasellus pulvinar libero sit amet ipsum commodo tempor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus eleifend est, non blandit lectus placerat ac. Cras vel fermentum magna. Nulla facilisi. Praesent vulputate lorem quis massa pulvinar interdum. Fusce mollis nisi vel mauris congue congue rhoncus arcu aliquam. Pellentesque nisl tellus, tristique at porttitor malesuada, aliquam eget enim. Donec malesuada pharetra sapien eu lobortis. Morbi tempus bibendum mi sit amet ornare. Nulla egestas, risus at feugiat sagittis, ligula odio laoreet urna, sed cursus orci turpis quis tortor. Fusce consequat ante vel nulla tempor eget consectetur urna fringilla. Donec cursus lorem at eros tempus laoreet.';
 
  var fail = 'PREFIXFAIL';
 
  var preparedRegexSuccess = /^he/
  var preparedRegexFail = /^PREFIXFAIL/
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
indexOf / success
origString.indexOf(success) === 0;
pending…
create regex, then test /success
var r = new RegExp('^' + success);
r.test(origString);
pending…
lastIndexOf / success
origString.lastIndexOf(success, 0) === 0;
pending…
substring / success
origString.substring(0, success.length) === success;
pending…
substr / success
origString.substr(0, success.length) === success;
pending…
indexOf / fail
longString.indexOf(fail) === 0;
pending…
create regex, then test / fail
var r = new RegExp('^' + fail);
r.test(longString);
pending…
lastIndexOf / fail
longString.lastIndexOf(fail, 0) === 0;
pending…
substring / fail
longString.substring(0, fail.length) === fail;
pending…
substr / fail
longString.substr(0, fail.length) === fail;
pending…
prepared regex / success
preparedRegexSuccess.test(longString);
pending…
prepared regex / fail
preparedRegexFail.test(longString);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment