Open theUpsider opened 2 years ago
This one's faster:
function isNumber64(num) {
switch (true) {
case num == null:
return false;
case typeof num === 'number':
return num - num === 0;
case typeof num === 'string' && num.trim().length > 0:
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
default:
return false;
}
}
Even better:
function isNumber65(num) {
switch (true) {
case typeof num === 'number':
return num - num === 0;
case typeof num === 'string':
for (let i = 0; i < num.length; i++) {
if (num.charCodeAt(i) !== 32 && num.charCodeAt(i) !== 13 && num.charCodeAt(i) !== 10 && num.charCodeAt(i) !== 9) {
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
}
}
return false;
default:
return false;
}
}
Extremely faster speeds on "all" tests. Also not significantly worse in individual tests.