RegExp: exec, match, test, search and indexOf

JavaScript performance comparison

Revision 26 of this test case created

Info

Longer StringsTests to determine which function is faster for detecting whether a string matches a regex pattern.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var abcRegex = /abc/;
    String.prototype.test = function(re){ return re.test(this); };
    String.prototype.test2 = function(re){ return this.search(re)>=0; };
   
    function getString() {
        return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx";
    }
};
</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
match array
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".match("abc|bcx") !==null ) {
 // String matches regex
}
pending…
match boolean
if (!!"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".match("abc|bcx") ) {
 // String matches regex
}
pending…
indexOf or indexOf
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("abc") != -1 || "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("bcx") != -1 ) {
}
pending…
double string compare
if (getString() == "111xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx" || getString() == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx" ) {
}
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