Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing even further #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cycle = (e, nl) => {
function bench(name) {
const suite = new Suite()
.on('start', () => console.log(`# ${name}`))
.on('complete', function(e) {
.on('complete', function (e) {
const fastest = this.filter('fastest').map('name').toString();
console.log(`fastest is '${fastest}'`);
console.log();
Expand All @@ -37,24 +37,30 @@ function run(fn, prop = 'all') {
}

bench('all')
.add('v6.3', () => run(isNumber63))
.add('v6.2', () => run(isNumber62))
.add('v6.1', () => run(isNumber61))
.add('v6.0', () => run(isNumber60))
.add('parseFloat', () => run(isNumberParseFloat))
.run()

bench('string')
.add('v6.3', () => run(isNumber63, '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.3', () => run(isNumber63, 'number'))
.add('v6.2', () => run(isNumber62, 'number'))
.add('v6.1', () => run(isNumber61, 'number'))
.add('v6.0', () => run(isNumber60, 'number'))
.add('parseFloat', () => run(isNumberParseFloat, 'number'))
.run()

function isNumberParseFloat(n) {
function isNumberParseFloat(num) {
if (typeof num === 'number') {
return num - num === 0;
}
Expand All @@ -80,7 +86,7 @@ function isNumber60(val) {
return false;
}

function isNumber61(val) {
function isNumber61(num) {
if (typeof num === 'number') {
return num - num === 0;
}
Expand All @@ -90,3 +96,26 @@ function isNumber61(val) {
return false;
}

function isNumber62(num) {
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;
}

function isNumber63(num) {
switch (typeof num) {
case 'number':
return num - num === 0;
case 'string':
if (num.trim() !== '')
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
return false;
default:
return false;
}
}
17 changes: 10 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

'use strict';

module.exports = function(num) {
if (typeof num === 'number') {
return num - num === 0;
module.exports = function (num) {
switch (typeof num) {
case 'number':
return num - num === 0;
case 'string':
if (num.trim() !== '')
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
return false;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is line 17 return false; needed?

default:
return false;
}
if (typeof num === 'string' && num.trim() !== '') {
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
}
return false;
};