Javascript begin with - indexOf vs substr

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

<script>
  Benchmark.prototype.setup = function() {
    var str = "http://url.url";
    var str2 = "ftp://url.url";
  };
</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
return (str.indexOf("http") == 0 && str2.indexOf("http") == 0);
pending…
substr
return (str.substr(0,4) == "http" && str2.substr(0,4) == "http");
pending…
lastIndexOf
return (str.lastIndexOf("http", 0) == 0 && str2.lastIndexOf("http", 0) == 0);
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