RegExp vs indexOf vs split

JavaScript performance comparison

Revision 13 of this test case created by weee

Info

Compare speed of Regex, indexOf and split for simple string comparison.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    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
var isPresent = new RegExp('[\\?|&]' + 'msn=.');
if ( isPresent.test(url) ) {
  // no op
}
pending…
indexOf
if ( -1 !== url.indexOf('?msn=') || -1 !== url.indexOf('&msn=') ) {
  // no op
}
pending…
split
if ( 2 === url.split('?msn=').length || 2 === url.split('&msn=').length ) {
  // no op
}
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