IsDigit() ASCII

Benchmark created on


Description

Checking if a character is a digit or not.

Setup

const testDigits = [1, 2, 3]
const testOthers = ["a", "*", "&"]
let digitsCount = 0

Test runner

Ready to run.

Testing in
TestOps/sec
Using switch/case
function isDigit(charCode) {
  switch (charCode) {
  case 48: case 49: case 50: case 51: case 52:
  case 53: case 54: case 55: case 56: case 57:
    return true
  default:
    return false
  }
}

for (const testDigit of testDigits)
  if (isDigit(testDigit))
  	digitsCount++
  	
for (const testOther of testOthers)
  if (isDigit(testOther))
    digitsCount++
ready
Using comparison
function isDigit(charCode) {
  return charCode >= 48 && charCode <= 57
}

for (const testDigit of testDigits)
  if (isDigit(testDigit))
  	digitsCount++
  	
for (const testOther of testOthers)
  if (isDigit(testOther))
    digitsCount++
ready
Using bitwise
function isDigit(charCode) {
  return (charCode - 48) >>> 0 < 10;
}

for (const testDigit of testDigits)
  if (isDigit(testDigit))
  	digitsCount++
  	
for (const testOther of testOthers)
  if (isDigit(testOther))
    digitsCount++
ready

Revisions

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