Integer Validity (v9)

Revision 9 of this benchmark created on


Preparation HTML

<script>
function isInt1(n) {
    return typeof n === "number" && (n % 1) === 0;
}

function isInt2(n) {
    return n === parseInt(n);
}

function isInt3(n) {
    return ~~n === n;
}

function isInt4(n) {
    return (n|0) === n;
}
</script>

Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
Typeof and modulus operator
if (!isInt1(a)) die
if (isInt1(b)) die
if (isInt1(c)) die
ready
Using parseInt
if (!isInt2(a)) die
if (isInt2(b)) die
if (isInt2(c)) die
ready
Using double tilde
if (!isInt3(a)) die
if (isInt3(b)) die
if (isInt3(c)) die
ready
Using |0
if (!isInt4(a)) die
if (isInt4(b)) die
if (isInt4(c)) die
ready

Revisions

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