regexp vs indexOf (v17)

Revision 17 of this benchmark created by sevifives on


Description

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

Mod:

Preparation HTML

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

Setup

var r;
    var reContains = /(?:^| )foo(?: |$)/;
    
    function dynamicRegExp(value) {
      return value.match(RegExp('(?:^| )foo(?: |$)'));
    }
    
    function inlineRegExp(value) {
      return value.match(/(?:^| )foo(?: |$)/);
    }
    
    function storedRegExp(value) {
      return value.match(reContains);
    }
    
    function stringIndexOf(value) {
      return 'foo'.indexOf(value) > -1
    }

Test runner

Ready to run.

Testing in
TestOps/sec
(long string) dynamic regexp
r = dynamicRegExp('really + long ? string * with spaces');
ready
(long string) inline regexp
r = inlineRegExp('really + long ? string * with spaces');
ready
(long string) stored regexp
r = storedRegExp('really + long ? string * with spaces');
ready
(long string) string indexOf
r = stringIndexOf('really + long ? string * with spaces');
ready

Revisions

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