shExpMatch

JavaScript performance comparison

Revision 5 of this test case created by tomByrer

Info

revision 5: Is "Normal Addition" faster than "+=" ? http://jsperf.com/additions/3

Preparation code

<script>
//"normal addition" http://jsperf.com/additions/3
  function shExpMatchNA(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 = 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 = urlIndex + 1;
        }
        isAggressive = false;
        urlIndex = 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 = urlIndex + 1;
      }
    }
    return urlIndex === urlLength;
  }

//revision 4 "PlusEquals" +=
  function shExpMatchPE(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…
shExpMatchPE
shExpMatchPE("http://platform.twitter.com/widgets.js", "*.t?tter.com/*")
// increment indexes += 1
pending…
shExpMatchNA
shExpMatchNA("http://platform.twitter.com/widgets.js", "*.t?tter.com/*")
// increment index = index + 1
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:

1 comment

tomByrer commented :

Firefox Beta 16.0a1 & PaleMoon 12.2.1 (FF optimized x64) both run on same i7 laptop.

Add a comment