Checking if a string is alphabetic (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script>
  var yesStr = "abcdefg abcdefg abcdefg random ABCDE";
  var noStr = "abcABC1234random.123#$%^&*";
  var upperBound = "A".charCodeAt(0);
  var lowerBound = "Z".charCodeAt(0);
  var upperBound1 = "a".charCodeAt(0);
  var lowerBound1 = "z".charCodeAt(0);
  var space = " ".charCodeAt(0);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Regex check, both cases
yesStr.search(/[^A-Za-z\s]/);
noStr.search(/[^A-Za-z\s]/);
ready
For loop
for (var i = 0; i < yesStr.length; i++) {
  var char = yesStr.charCodeAt(i);
  if (char <= upperBound && char >= lowerBound) continue;
  else if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (yesStr[i] == " ") continue;
  else break;
}

for (var i = 0; i < noStr.length; i++) {
  var char = noStr.charCodeAt(i);
  if (char <= upperBound && char >= lowerBound) continue;
  else if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (noStr[i] == " ") continue;
  else break;
}
ready
For loop and toLowerCase
yesStrTemp = yesStr.toLowerCase();
for (var i = 0; i < yesStrTemp.length; i++) {
  var char = yesStrTemp.charCodeAt(i);
  if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (yesStrTemp[i] == " ") continue;
  else break;
}

noStrTemp = noStr.toLowerCase();
for (var i = 0; i < noStrTemp.length; i++) {
  var char = noStrTemp.charCodeAt(i);
  if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (noStrTemp[i] == " ") continue;
  else break;
}
ready
Regex check w/ ignore case flag
yesStr.search(/[^a-z\s]/i);
noStr.search(/[^a-z\s]/i);
ready
For loop space code
for (var i = 0; i < yesStr.length; i++) {
  var char = yesStr.charCodeAt(i);
  if (char <= upperBound && char >= lowerBound) continue;
  else if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (char === space) continue;
  else break;
}

for (var i = 0; i < noStr.length; i++) {
  var char = noStr.charCodeAt(i);
  if (char <= upperBound && char >= lowerBound) continue;
  else if (char <= upperBound1 && char >= lowerBound1) continue;
  else if (char === space) continue;
  else break;
}
ready

Revisions

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