String.startsWith (v71)

Revision 71 of this benchmark created by JoePeck on


Description

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

Preparation HTML

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

Setup

String.prototype.startsWith1 = function(s) {
      return this.indexOf(s) === 0;
    }
    
    String.prototype.startsWith2 = function(s) {
      return this.lastIndexOf(s, 0) === 0;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
found short regexp
/^http/.test(shortStr);
ready
found short indexOf
shortStr.startsWith1("http");
ready
found short lastIndexOf
shortStr.startsWith2("http");
ready
missing short regexp
/^missing/.test(shortStr);
 
ready
missing short indexOf
shortStr.startsWith1("missing");
ready
missing short lastIndexOf
shortStr.startsWith2("missing");
ready
found long regexp
/^Lorem/.test(longStr);
ready
found long indexOf
longStr.startsWith1("Lorem");
 
ready
found long lastIndexOf
longStr.startsWith2("Lorem");
 
ready
missing long regex
/^missing/.test(longStr);
ready
missing long indexOf
longStr.startsWith1("missing");
 
ready
missing long lastIndexOf
longStr.startsWith2("missing");
ready

Revisions

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