String.startsWith

JavaScript performance comparison

Revision 71 of this test case created by JoePeck

Info

Different possible implementations of string.startsWith(...);

Preparation code

<script>
var shortStr = "http://example.com";
var longStr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam volutpat aliquam erat nec scelerisque. Suspendisse potenti. Aenean vel dapibus turpis. Vestibulum vitae dolor justo, ullamcorper posuere mi. Fusce porta congue blandit. Integer tempus lacus sed lorem luctus rutrum. Vivamus et enim at odio faucibus lacinia. Aenean rutrum dui quis dolor lacinia ultricies. Vivamus sed ipsum magna, et porttitor dui. In sed massa eget felis dapibus facilisis id eget nulla. Suspendisse libero ante, viverra ut eleifend sed, consectetur ac massa. Vivamus mollis ipsum a felis egestas rutrum. Nulla est metus, bibendum eu ullamcorper et, tempor sed urna. Mauris non velit at dolor aliquet dignissim. Sed ligula mauris, aliquam vel dictum at, tempus quis ipsum.";
</script>
<script>
Benchmark.prototype.setup = function() {
    String.prototype.startsWith1 = function(s) {
      return this.indexOf(s) === 0;
    }
   
    String.prototype.startsWith2 = function(s) {
      return this.lastIndexOf(s, 0) === 0;
    }
   
};
</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
found short regexp
/^http/.test(shortStr);
pending…
found short indexOf
shortStr.startsWith1("http");
pending…
found short lastIndexOf
shortStr.startsWith2("http");
pending…
missing short regexp
/^missing/.test(shortStr);
 
pending…
missing short indexOf
shortStr.startsWith1("missing");
pending…
missing short lastIndexOf
shortStr.startsWith2("missing");
pending…
found long regexp
/^Lorem/.test(longStr);
pending…
found long indexOf
longStr.startsWith1("Lorem");
 
pending…
found long lastIndexOf
longStr.startsWith2("Lorem");
 
pending…
missing long regex
/^missing/.test(longStr);
pending…
missing long indexOf
longStr.startsWith1("missing");
 
pending…
missing long lastIndexOf
longStr.startsWith2("missing");
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