regexp vs indexOf

JavaScript performance comparison

Revision 94 of this test case created by SpeedySan

Info

Usually .match and .indexOf are used in condition statements where the result of both of these has to be converted to a boolean. Since one of these returns an array (or null) and another returns a number, it would be more useful if we also included the cost of converting these results to a boolean in the final result.

Also, .match returns either an array or a null, which have different conversion costs. So added negative tests as well.

Preparation code

<script>
  var str = "hello world!";
</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
Regexp Positive
!!str.match(/world/);
pending…
indexOf Positive
str.indexOf("world")>-1;
pending…
Regexp Negative
!!str.match(/nomatch/);
pending…
indexOf Negative
str.indexOf("nomatch")>-1;
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