Alphanumeric (charCode vs RegExp)

JavaScript performance comparison

Test case created by Michael Martin-Smucker

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var text = 'This is a much, much longer string that will have a character at the end that will not pass the validation!';

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
charCode
function isAlphaNumeric(str) {
  var code;

  for (var i = 0, len = str.length; i < len; i++) {
    code = str.charCodeAt(i);
    if (!(code > 47 && code < 58) && // numeric (0-9)
        !(code > 64 && code < 91) && // upper alpha (A-Z)
        !(code > 96 && code < 123)) { // lower alpha (a-z)
      return false;
    }
  }
  return true;
}

isAlphaNumeric(text);
pending…
regexp
function isAlphaNumeric(str) {
  return /^[a-zA-Z0-9]+$/.test(str);
}

isAlphaNumeric(text);
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.

0 Comments