shExpMatch

JavaScript performance comparison

Revision 4 of this test case created by ayanamist

Preparation code

<script>
  function shExpMatch(url, pattern) {
    var pCharCode;
    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) {
      pCharCode = pattern.charCodeAt(pIndex); // use charCodeAt for performance, see http://jsperf.com/charat-charcodeat-brackets
      if (pCharCode === 63) { // use if instead of switch for performance, see http://jsperf.com/switch-if
        if (isAggressive) {
          urlIndex += 1;
        }
        isAggressive = false;
        urlIndex += 1;
      } else if (pCharCode === 42) {
        if (pIndex === patternLength - 1) {
          return urlIndex <= urlLength;
        } else {
          isAggressive = true;
        }
      } else {
        if (isAggressive) {
          lastIndex = urlIndex;
          urlIndex = url.indexOf(String.fromCharCode(pCharCode), lastIndex + 1);
          if (urlIndex < 0) {
            if (url.charCodeAt(lastIndex) !== pCharCode) {
              return false;
            }
            urlIndex = lastIndex;
          }
          isAggressive = false;
        } else {
          if (urlIndex >= urlLength || url.charCodeAt(urlIndex) !== pCharCode) {
            return false;
          }
        }
        urlIndex += 1;
      }
    }
    return urlIndex === urlLength;
  }

  var shExpMatchOrig = 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
Original shExpMatch
shExpMatchOrig("http://platform.twitter.com/widgets.js", "*.t?tter.com/*")
pending…
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