Integer Validity (v2)

Revision 2 of this benchmark created on


Setup

var a = 12;
    var b = 34.5;
    var c = Infinity;
    
    var typeOf = (function (toString, regExp) {
       return function(obj) {
         return toString.call(obj).match(regExp)[1].toLowerCase();
       };
    }(Object.prototype.toString, /\[object (\w+)\]/));

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 safe typeof
typeOf(a) == 'number' && a % 1 == 0;
typeOf(b) == 'number' && b % 1 == 0;
typeOf(c) == 'number' && c % 1 == 0;
ready
Using bit shift operator
a >> 0 === a
b >> 0 === b
c >> 0 === c
ready
Using bitwise not
~~a === a
~~b === b
~~c === c
ready

Revisions

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