regexp vs indexOf (v15)

Revision 15 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 element = document.getElementById('foo');
    var value = " ".concat(element.className).concat(" ");
    var reContains = /(?:^| )foo(?: |$)/;
    var dynamicContains = new RegExp('(?:^| )foo(?: |$)');
    
    function dynamicRegExp(value) {
      return RegExp('(?:^| )foo(?: |$)').test(value);
    }
    
    function inlineRegExp(value) {
      return /(?:^| )foo(?: |$)/.test(value);
    }
    
    function dynamicStoredRegExp(value) {
      return dynamicContains.test(value);
    }
    
    function storedRegExp(value) {
      return reContains.test(value);
    }
    
    function stringIndexOf(node) {
      return value.indexOf(' foo ') > -1;
    }

Test runner

Ready to run.

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

Revisions

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