regex-multi-test-random (v3)

Revision 3 of this benchmark created by MoonScript on


Description

Multiple regular expression .test() calls using if/else-if, or a single string.match() call. Modified to randomly grab a different string for every loop iteration, instead of using the same string for each test.

Preparation HTML

<script>
  var q1RegExp = /^select hostheader,referer,firstbyte/;
  var q2RegExp = /^select "Q1/;
  var q3RegExp = /^select "Q2/;
  var q4RegExp = /^select "Q3/;
  var singleRegExp = /^select ("Q\d+|hostheader,referer,firstbyte)/;
  var QueryNames = ['select hostheader,referer,firstbyte','select "Q1','select "Q2','select "Q3'];
  var totalQueryNames = QueryNames.length;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex.test() with if/else-if
var QueryName = QueryNames[Math.floor(Math.random()*totalQueryNames)];
if (q1RegExp.test(QueryName)) {
  void(1);
} else if (q2RegExp.test(QueryName)) {
  void(2);
} else if (q3RegExp.test(QueryName)) {
  void(3);
} else if (q4RegExp.test(QueryName)) {
  void(4);
}
ready
string.match() with switch
var QueryName = QueryNames[Math.floor(Math.random()*totalQueryNames)];
switch (QueryName.match(singleRegExp)[1]) {
  case 'hostheader,referer,firstbyte' :
    void(1);
    break;
  case '"Q1' :
    void(2);
    break;
  case '"Q2' :
    void(3);
    break;
  case '"Q3' :
    void(4);
    break;
}
ready

Revisions

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

  • Revision 1: published by MoonScript on
  • Revision 2: published by Dan Dockery on
  • Revision 3: published by MoonScript on