Skip to content

Optimizing even further#36

Open
theUpsider wants to merge 3 commits into
jonschlinkert:masterfrom
theUpsider:optimize63
Open

Optimizing even further#36
theUpsider wants to merge 3 commits into
jonschlinkert:masterfrom
theUpsider:optimize63

Conversation

@theUpsider
Copy link
Copy Markdown

image
Extremely faster speeds on "all" tests. Also not significantly worse in individual tests.

juszczykjakub and others added 3 commits July 11, 2019 22:16
Comment thread index.js
case 'string':
if (num.trim() !== '')
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
return false;
Copy link
Copy Markdown

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?

@EdwardDrapkin
Copy link
Copy Markdown

EdwardDrapkin commented Mar 3, 2024

This one's faster:

image

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:

image

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;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants