RegExp: char class vs alternation

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var chars = 'abcdefghijklmnopqrstuvwxyz';
    var regexCls =
      /.*([abcdefghijklmnopqrstuvwxyz])\.txt/;
    var regexAlt =
      /.*(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)\.txt/;
    var strs = [];
    var len;
    for (var i = 0; i < len; i++) {
      strs.push('bbbb' + chars.charAt(i) + '.txt');
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
RegExp Character Class
var len = strs.length;
for (var i = 0; i < len; i++) {
  regexCls.exec(strs[i]);
}
pending…
RegExp Alternation
var len = strs.length;
for (var i = 0; i < len; i++) {
  regexAlt.exec(strs[i]);
}
pending…

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

Compare results of other browsers

0 comments

Add a comment