startsWith (v11)

Revision 11 of this benchmark created on


Setup

a = ["test"];
    for (var i = 0; i < 10000; i++) {
      a.push("some other stuff");
    }
    s = a.join();

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
r1 = (s.indexOf("test") == 0);
r2 = (s.indexOf("not there") == 0);
ready
lastIndexOf
r1 = (s.lastIndexOf("test", 0) == 0);
r2 = (s.lastIndexOf("not there", 0) == 0);
ready
substring
r1 = (s.substring(0, "test".length) == "test");
r2 = (s.substring(0, "not there".length) == "not there");
ready
slice
r1 = (s.slice(0, "test".length) == "test");
r2 = (s.slice(0, "not there".length) == "not there");
ready
regex
r1 = (/^test/).test(s);
r2 = (/^not there/).test(s);
ready
letter-by-letter
function startsWith(str,substr) {
  for (var i = 0; i < substr.length; ++i) {
    if (!str[i] || str[i] != substr[i]) return false;
  }
  return true;
}
r1 = startsWith(s,"test");
r2 = startsWith(s,"not there");
 
ready

Revisions

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