Integer Validity (v18)

Revision 18 of this benchmark created on


Setup

var a = 12;
    var b = 34.5;
    var c = Infinity;

Test runner

Ready to run.

Testing in
TestOps/sec
Typeof and modulus operator
(typeof a === 'number' && (a % 1) === 0);
(typeof b === 'number' && (b % 1) === 0);
(typeof c === 'number' && (c % 1) === 0);
ready
Using parseInt
(a == parseInt(a));
(b == parseInt(b));
(c == parseInt(c));
ready
using isFinite
(isFinite(a) && (a % 1) === 0);
(isFinite(b) && (b % 1) === 0);
(isFinite(c) && (c % 1) === 0);
ready
Using isNaN and parseInt
!isNaN(parseInt(a, 0));
!isNaN(parseInt(b, 0));
!isNaN(parseInt(c, 0));
ready
Using regex
/^-?\+?\d+$/g.test(a);
/^-?\+?\d+$/g.test(b);
/^-?\+?\d+$/g.test(c);
ready
Using a stupid isInteger function
function isInteger(val) {
  if (val == null) {
    return false;
  }
  if (val.length == 0) {
    return false;
  }
  for (var i = 0; i < val.length; i++) {
    var ch = val.charAt(i);
    if (i == 0 && ch == "-") {
      continue;
    }
    if (ch < "0" || ch > "9") {
      return false;
    }
  }
  return true;
}

isInteger(a);
isInteger(b);
isInteger(c);
ready

Revisions

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