From cb026e9448a06de5feecb81fda35be39729ff22b Mon Sep 17 00:00:00 2001 From: Kuba Juszczyk Date: Thu, 11 Jul 2019 22:16:02 +0200 Subject: [PATCH] Optimize --- benchmark/index.js | 13 +++++++++++++ index.js | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/benchmark/index.js b/benchmark/index.js index a2f4b9e..26a1491 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -37,18 +37,21 @@ function run(fn, prop = 'all') { } bench('all') + .add('v6.2', () => run(isNumber62)) .add('v6.1', () => run(isNumber61)) .add('v6.0', () => run(isNumber60)) .add('parseFloat', () => run(isNumberParseFloat)) .run() bench('string') + .add('v6.2', () => run(isNumber62, 'string')) .add('v6.1', () => run(isNumber61, 'string')) .add('v6.0', () => run(isNumber60, 'string')) .add('parseFloat', () => run(isNumberParseFloat, 'string')) .run() bench('number') + .add('v6.2', () => run(isNumber62, 'number')) .add('v6.1', () => run(isNumber61, 'number')) .add('v6.0', () => run(isNumber60, 'number')) .add('parseFloat', () => run(isNumberParseFloat, 'number')) @@ -90,3 +93,13 @@ function isNumber61(val) { return false; } +function isNumber62(val) { + var type = typeof num; + if (type === 'number') { + return num - num === 0; + } + if (type === 'string' && num.trim() !== '') { + return Number.isFinite ? Number.isFinite(+num) : isFinite(+num); + } + return false; +} diff --git a/index.js b/index.js index 27f19b7..cba4acf 100644 --- a/index.js +++ b/index.js @@ -8,10 +8,11 @@ 'use strict'; module.exports = function(num) { - if (typeof num === 'number') { + var type = typeof num; + if (type === 'number') { return num - num === 0; } - if (typeof num === 'string' && num.trim() !== '') { + if (type === 'string' && num.trim() !== '') { return Number.isFinite ? Number.isFinite(+num) : isFinite(+num); } return false;