RegExp vs indexOf

JavaScript performance comparison

Revision 11 of this test case created by Marco

Info

Compare speed of Regex with indexOf for simple string comparison

Preparation code

<script src="http://underscorejs.org/underscore-min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    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];
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
a bunch of stuff
withStuff(test1);
withStuff(test2);
withStuff(test3);
pending…
regex
withRegex(test1);
withRegex(test2);
withRegex(test3);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Comment form temporarily disabled.

Add a comment