RegExp test / search / match vs. indexOf

JavaScript performance comparison

Revision 73 of this test case created by Robert

Preparation code

<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>
      
<script>
Benchmark.prototype.setup = function() {
  var precompiledRegex = /(inline|ruby)/i;
  var newRegex =  new RegExp('(inline|ruby)', 'i');

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

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

0 Comments