RegExp vs indexOf (v11)

Revision 11 of this benchmark created by Marco on


Description

Compare speed of Regex with indexOf for simple string comparison

Preparation HTML

<script src="http://underscorejs.org/underscore-min.js"></script>

Setup

var reg = /(.*?)(?:[:#.]|$)/
    var test1 = 'asasas:asasas#sasas',
      test2 = 'a.ss',
      test3 = 'addddddddddd#ss';
    
    function withStuff(str) {
      var found, founds
        foundColon = str.indexOf(':'),
        foundHash = str.indexOf('#'),
        foundStop = str.indexOf('.');
      founds = [foundColon, foundHash, foundStop];
      found = _.find(founds, function(found) {
        return found !== -1;
      });
      return str.slice(0, found);
    }
    
    function withRegex(str) {
      return reg.exec(str)[1];
    }

Test runner

Ready to run.

Testing in
TestOps/sec
a bunch of stuff
withStuff(test1);
withStuff(test2);
withStuff(test3);
ready
regex
withRegex(test1);
withRegex(test2);
withRegex(test3);
ready

Revisions

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