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

Conversation

theUpsider
Copy link

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

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?

@EdwardDrapkin
Copy link

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.

None yet

6 participants