RegExp vs indexOf

JavaScript performance comparison

Revision 18 of this test case created by Thiago Nobre

Info

Compare speed of Regex with indexOf for simple string comparison

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var isPresent = new RegExp('[\\?|&]' + 'msn=.');
    var url = "http://create.leadid.local?pid=2iuegu12&msn=23uigqwud";
};
</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
Regex
if (isPresent.test(url)) {
  // no op
}
pending…
indexOf
if (-1 !== url.indexOf('?msn=') || -1 !== url.indexOf('&msn=')) {}
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

Comment form temporarily disabled.

Add a comment