regexp vs indexOf (v13)

Revision 13 of this benchmark created on


Description

Checking the performance cost of using a regexp instead of String#indexOf.

Preparation HTML

<div id="foo" class="a foo bar"></div>

Setup

var r;
    var str = 'a foo bar';
    var reContains = /(?:^| )foo(?: |$)/;
    
    function dynamicRegExp() {
      return RegExp('(?:^| )foo(?: |$)').test(str);
    }
    
    function inlineRegExp() {
      return /(?:^| )foo(?: |$)/.test(str);
    }
    
    function storedRegExp() {
      return reContains.test(str);
    }
    
    function stringIndexOf() {
      return str.indexOf(' foo ') !== -1;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
dynamic regexp
r = dynamicRegExp();
ready
inline regexp
r = inlineRegExp();
ready
stored regexp
r = storedRegExp();
ready
string indexOf
r = stringIndexOf();
ready

Revisions

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