handling precomputed permutations

JavaScript performance comparison

Test case created by Jay Cox

Info

I am testing how fast the following two regexes are. Which one is faster?

Test 1

var reg1 = /dogs|dosg|dgos|dgso|dsog|dsgo|odsg|odgs|ogsd|ogds|osgd|osdg|gdos|gdso|gods|gosd|gsdo|gsod|sdgo|sdog|sogd|sodg|sgod|sgdo/g; 

Test 2

var reg2 = /d(?:o(?:gs|sg)|g(?:os|so)|s(?:go|og))|o(?:d(?:gs|sg)|g(?:ds|sd)|s(?:gd|dg))|g(?:o(?:ds|sd)|d(?:os|so)|s(?:do|od))|s(?:o(?:gd|dg)|g(?:od|do)|d(?:go|og))/g

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var
    match = null,
      v = ["d", "o", "g", "s"],
      i = 0,
      len = 10000,
      samp = [],
      str = "";
    var nummatches1 = 0;
    var nummatches2 = 0;
    for (i = 0; i < len; i++) {
      samp.push(v[Math.floor(Math.random() * 4)]);
    }
    str = samp.join("");
    var reg1 = /dogs|dosg|dgos|dgso|dsog|dsgo|odsg|odgs|ogsd|ogds|osgd|osdg|gdos|gdso|gods|gosd|gsdo|gsod|sdgo|sdog|sogd|sodg|sgod|sgdo/g;
    var reg2 = /d(?:o(?:gs|sg)|g(?:os|so)|s(?:go|og))|o(?:d(?:gs|sg)|g(?:ds|sd)|s(?:gd|dg))|g(?:o(?:ds|sd)|d(?:os|so)|s(?:do|od))|s(?:o(?:gd|dg)|g(?:od|do)|d(?:go|og))/g;
};
</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
no decision tree
  while (match = reg1.exec(str)) {
    nummatches1++;
  }
pending…
decision tree
  while (match = reg2.exec(str)) {
    nummatches2++;
  }
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