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

numberToString - return string directly #22300

Closed
wants to merge 4 commits into from

Conversation

ttodua
Copy link
Member

@ttodua ttodua commented Apr 27, 2024

No description provided.

@@ -39,6 +39,7 @@ const precisionConstants = {

function numberToString (x) { // avoids scientific notation for too large and too small numbers
if (x === undefined) return undefined;
if (typeof x === 'string') return x;
Copy link
Member

Choose a reason for hiding this comment

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

This seems unnecessary and actually less efficient, seeing as it does a string comparison https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString#description

The String object overrides the toString method of Object; it does not inherit Object.prototype.toString(). For String values, the toString method returns the string itself (if it's a primitive) or the string that the String object wraps. It has the exact same implementation as String.prototype.valueOf().

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure it's less efficient, you can test out here: https://jsbench.me/91lvmdl2e3/1
also tried other testers, everywhere this approach wins

Copy link
Member Author

Choose a reason for hiding this comment

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

@carlosmiei should we close this PR? is numberToString always applied against numerics for sure? does it anywhere depend on users ex.number setting?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why this approach would be a gain, because the toString method of a string just returns the string itself, so having another conditional just seems like extra, but this is such a minor thing that it doesn't really matter

@ttodua ttodua closed this Apr 30, 2024
@ttodua ttodua deleted the stringified-nums branch April 30, 2024 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants