startsWith

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  a = ["test"];
  for (var i = 0; i < 10000; i++) {
    a.push("some other stuff");
  }
  s = a.join();
  re1 = new RegExp("^test");
  re2 = new RegExp("^not there");

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
indexOf
r1 = (s.indexOf("test") == 0);
r2 = (s.indexOf("not there") == 0);
pending…
lastIndexOf
r1 = (s.lastIndexOf("test", 0) == 0);
r2 = (s.lastIndexOf("not there", 0) == 0);
pending…
substring
r1 = (s.substring(0, "test".length) == "test");
r2 = (s.substring(0, "not there".length) == "not there");
pending…
slice
r1 = (s.slice(0, "test".length) == "test");
r2 = (s.slice(0, "not there".length) == "not there");
pending…
regex
r1 = (/^test/).test(s);
r2 = (/^not there/).test(s);
pending…
compiled regex
r1 = re1.test(s);
r2 = re2.test(s);
pending…
charAt
    function startsWithCharAt(string, pattern) {
      for (var i = 0, length = pattern.length; i < length; i += 1) {
        if (pattern.charAt(i) !== string.charAt(i)) return false;
      }
      return true;
    }

r1 = startsWithCharAt(s, "test");
r2 = startsWithCharAt(s, "not there");
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.

0 Comments