regexp vs indexOf (v10)

Revision 10 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 cacheRegexp = {};
    function cachedRegExp(node, _class) {
      return (cacheRegexp[_class] || (cacheRegexp[_class] = RegExp('(?:^| )'+ _class +'(?: |$)'))).test(node.className);
    }
    
    var cacheClassName = {};
    function cachedIndexOf(node, _class) {
      var className = node.className;
      return (cacheClassName[className] || (cacheClassName[className] = ' '+ className +' ')).indexOf(cacheClassName[_class] || (cacheClassName[_class] = ' '+ _class +' ')) > -1;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
regexp
r = cachedRegExp(element, 'foo');
ready
indexOf
r = cachedIndexOf(element, 'foo');
ready

Revisions

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