IsDigit() ASCII (v2)

Revision 2 of this 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++
    
// ensure not optimized away
console.log(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++
    
// ensure not optimized away
console.log(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++

// ensure not optimized away
console.log(digitsCount)
ready

Revisions

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