RegExp test / search / match vs. indexOf (v73)

Revision 73 of this benchmark created by Robert on


Description

Testing for the existence of characters in a string using Regular Expressions test, search, and match compared to indexOf.

Added case sensitivity to search

Preparation HTML

<script>
  var str = 'INLINE-block';
  var arr = ['inline', 'inline-block', 'inline-table', 'ruby','large','array','div','with','a','lot','of','content','that','may','cause','some','issues','to','be','seen','later','on','or','perhaps','none','will','be','found','at','all','we','will','certainly','see','and','such'];
  var obj = {'inline': true, 'inline-block': true, 'inline-table': true, 'ruby': true};
</script>

Setup

var precompiledRegex = /(inline|ruby)/i;
    var newRegex =  new RegExp('(inline|ruby)', 'i');

Test runner

Ready to run.

Testing in
TestOps/sec
test
/(inline|ruby)/i.test(str);
ready
search
str.search(/(inline|ruby)/i) > -1;
ready
match
str.match(/(inline|ruby)/i).length > 0;
ready
indexOf
str.indexOf(/(inline|ruby)/i) > -1;
ready
precompiled test
precompiledRegex.test(str);
ready
precompiled search
str.search(precompiledRegex) > -1;
ready
precompiled match
str.match(precompiledRegex).length > 0;
ready
precompiled indexOf
str.indexOf(precompiledRegex) > -1;
ready
indexOf string primitive
str.toLowerCase().indexOf('inline') > -1 || str.toLowerCase().indexOf('ruby') > -1;
ready
Primitive comparison
str.toLowerCase() === "inline" || str.toLowerCase() === "inline-block" || str.toLowerCase() === "inline-table" || str.toLowerCase() === "ruby";
ready
Array.indexOf
arr.indexOf(str.toLowerCase()) > -1;
ready
Object-based lookup
obj[str.toLowerCase()] === true;
ready
Array forloop check
var c = str.toLowerCase();
for (var i = 0; i < arr.length; i++) {
    if (arr[i] === c) {
        return true;
    }
}
return false;
ready
new RegExp
newRegex.test(str);
ready

Revisions

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