Char code vs regex

Benchmark created on


Setup

function isAlphaNumericA(char) {
  const code = char.charCodeAt(0);
  return (
    (code >= 48 && code <= 57) || // 0-9
    (code >= 65 && code <= 90) || // A-Z
    (code >= 97 && code <= 122) // a-z
  );
}

function isAlphaNumericB(char) {
  return /[a-zA-Z0-9]/.test(char);
}

Test runner

Ready to run.

Testing in
TestOps/sec
Char code
isAlphaNumericA('a');
isAlphaNumericA('L');
isAlphaNumericA('7');
isAlphaNumericA('(');
ready
Regex
isAlphaNumericB('a');
isAlphaNumericB('L');
isAlphaNumericB('7');
isAlphaNumericB('(');
ready

Revisions

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