jQuery.isNumeric (v3)

Revision 3 of this benchmark created on


Description

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

Preparation HTML

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

Setup

rdigit = /\d/;
    
    jQuery.isNumeric2 = function( obj ) {
        return obj !== null && !isNaN( obj ) && rdigit.test( obj );
    };

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

Revisions

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