RegExp: exec, match, test, search and indexOf

JavaScript performance comparison

Revision 25 of this test case created by Joe

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; };
   
};
</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
exec()
if (abcRegex.exec("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx") !== null) {
 // String matches regex
}
pending…
match()
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".match(abcRegex) !== null) {
 // String matches regex
}
pending…
test()
if (abcRegex.test("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx") !== false) {
 // String matches regex
}
pending…
search()
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".search(abcRegex) !== -1) {
 // String matches regex
}
pending…
indexOf()
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("abc") > -1) {
 // String matches regex
}
pending…
match() str
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".match("abc") ) {
 // String matches regex
}
pending…
test(re)
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".test(abcRegex)) {
 // String matches regex
}
pending…
searchtext
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".search("abc") !== -1) {
 // String matches regex
}
pending…
indexOf 2
if ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("abc") !== -1) {
 // String matches regex
}
pending…
indexOf 3
if (!"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("abc")) {
 // String matches regex
}
pending…
indexOf 4
if (~"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxabcxxxxxxabcxx".indexOf("abc")) {
 // String matches regex
}
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