shExpMatch

JavaScript performance comparison

Revision 2 of this test case created by ayanamist

Preparation code

<script>
  function shExpMatch(url, pattern) {
    var pChar;
    var isAggressive = false;
    var pIndex;
    var urlIndex = 0;
    var lastIndex;
    var patternLength = pattern.length;
    var urlLength = url.length;
    for (pIndex = 0; pIndex < patternLength; pIndex += 1) {
      pChar = pattern[pIndex];
      switch (pChar) {
      case "?":
        if (isAggressive) {
          urlIndex += 1;
        }
        isAggressive = false;
        urlIndex += 1;
        break;
      case "*":
        if (pIndex === patternLength - 1) {
          return urlIndex <= urlLength;
        } else {
          isAggressive = true;
        }
        break;
      default:
        if (isAggressive) {
          lastIndex = urlIndex;
          urlIndex = url.indexOf(pChar, lastIndex + 1);
          if (urlIndex < 0) {
            if (url[lastIndex] !== pChar) {
              return false;
            }
            urlIndex = lastIndex;
          }
          isAggressive = false;
        } else {
          if (urlIndex >= urlLength || url[urlIndex] !== pChar) {
            return false;
          }
        }
        urlIndex += 1;
      }
    }
    return urlIndex === urlLength;
  }

  var shExpMatch2 = function(url, pattern) {
      pattern = pattern.replace(/\./g, '\\.');
      pattern = pattern.replace(/\*/g, '.*');
      pattern = pattern.replace(/\?/g, '.');
      var newRe = new RegExp('^' + pattern + '$');
      return newRe.test(url);
      }
</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
Old shExpMatch
shExpMatch2("http://platform.twitter.com/widgets.js", "*.t?tter.com/*")
pending…
New shExpMatch
shExpMatch("http://platform.twitter.com/widgets.js", "*.t?tter.com/*")
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

Add a comment