jQuery.isNumeric (v4)

Revision 4 of this benchmark created on


Description

'&&' is lazy, checking !isNaN before running a regex could be quicker.

Preparation HTML

<script src="//code.jquery.com/jquery-1.7.js"></script>

Setup

rdigit = /\d/;
    
    jQuery.isNumeric2 = function( obj ) {
        return obj !== null && !isNaN( obj ) && rdigit.test( obj );
    };
    
    jQuery.isNumeric3= function(value){
     return NaN!==value && 'number'===typeof(value);
    }
    
    var num= 'number';
    jQuery.isNumeric4= function(value){
     return NaN!==value && num===typeof(value);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Base
jQuery.isNumeric(NaN);
ready
Swap Arguments
jQuery.isNumeric2(NaN);
ready
Compare Entire Set
var values = ["-10", 16, 0xFF, "0xFF", "8e5", 3.1415, +10, 0144, "", {}, NaN, null, true, Infinity, undefined];
for (var i = 0; i < values.length; i++) {
    $.isNumeric(values[i]);
}
ready
Compare Entire Set Swapped
var values = ["-10", 16, 0xFF, "0xFF", "8e5", 3.1415, +10, 0144, "", {}, NaN, null, true, Infinity, undefined];
for (var i = 0; i < values.length; i++) {
    $.isNumeric2(values[i]);
}
ready
regex-less
var values = ["-10", 16, 0xFF, "0xFF", "8e5", 3.1415, +10, 0144, "", {}, NaN, null, true, Infinity, undefined];
for (var i = 0; i < values.length; i++) {
    $.isNumeric3(values[i]);
}
ready
regex-less cached strings
var values = ["-10", 16, 0xFF, "0xFF", "8e5", 3.1415, +10, 0144, "", {}, NaN, null, true, Infinity, undefined];
for (var i = 0; i < values.length; i++) {
    $.isNumeric4(values[i]);
}
ready

Revisions

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