Integer Validity (v4)

Revision 4 of this benchmark created on


Setup

var a = 12;
    var b = 34.5;
    var c = "hello";
    var d = null;
    var e = NaN;
    
    var arr = [];
    for (var j=0; j< 100000; j++){
      arr.push(j);
    }
    
    var checkInt = function(arr) {
      var len = arr.length;
      for (var i=0; i<len; i++) {
        if ((typeof arr[i]) !=='number' && ((arr[i])%1)!==0); {throw Error('all elements must be integers');}
      }
    };
    
    var checkInt2 = function(arr) {
      var len = arr.length;
      for (var i=0; i<len; i++) {
        if (arr[i] != parseInt(arr[i])); {throw Error('all elements must be integers');}
      }
    };

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);
(typeof d==='number' && (d%1)===0);
(typeof e==='number' && (e%1)===0);

//checkInt(arr);
ready
Using parseInt
(a == parseInt(a));
(b == parseInt(b));
(c == parseInt(c));
(d == parseInt(d));
(e == parseInt(e));

checkInt2(arr);
ready

Revisions

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