exec() vs match() vs test() vs search() vs indexOf() (v38)

Revision 38 of this benchmark created by irontrooper on


Description

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

Preparation HTML

<label id="deneme"></label>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
exec()
var labelId = $("label").attr("id");

if (labelId.substring(4) == "labe") {
 // String matches 
}
ready
match()
if ("xxxxxxxxxabcxxxxxxabcxx".match(/abc/) !== null) {
 // String matches regex
}
ready
test()
var labelId = $("label").attr("id");
if (labelId.indexOf("labe")) {
 // String matches regex
}
ready
search()
if (("xxxxxxxxxabcxxxxxxabcxx").search(/abc/) !== -1) {
 // String matches regex
}
ready
indexOf()
if ("xxxxxxxxxabcxxxxxxabcxx".indexOf("abc") !== -1) {
 // String matches regex
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.