regexp vs indexOf

JavaScript performance comparison

Revision 3 of this test case created by Dave Tonge

Preparation code

<script>
  var haystack = "heLlo world!";
  var needle = "LO";
</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
var pattern = new RegExp(needle,"gi");
var result = pattern.test(haystack);
pending…
indexOf
var haystack_lower = haystack.toLowerCase();
var needle_lower = needle.toLowerCase();
var result = haystack_lower.indexOf(needle_lower) !== -1;
pending…
RegExp no global
var pattern = new RegExp(needle,"i");
var result = pattern.test(haystack);
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