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

[misc] Use switch-case for better get-set performance (#2659) #5718

Closed
wants to merge 2 commits into from

Conversation

tomerle
Copy link
Contributor

@tomerle tomerle commented Sep 15, 2020

Also done for setMonth.
Also simplified Leap Day logic.

Also done for `setMonth`.
Also simplified Leap Day logic.
@coveralls
Copy link

coveralls commented Sep 15, 2020

Coverage Status

Coverage increased (+0.002%) to 88.526% when pulling c8cb238 on tomerle:get-set-switch-case into 471d67f on moment:develop.

Copy link
Member

@marwahaha marwahaha left a comment

Choose a reason for hiding this comment

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

@ichernev may also want to look at this

d = mom._d;
isUTC = mom._isUTC;

switch (unit) {
Copy link
Member

Choose a reason for hiding this comment

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

How did you verify all units that are used in the getter and setter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function get(mom, unit) is actually only used internally.
The public method mom.get(unit) is actually the function stringGet(unit) which checks for this.
This is exactly what protected the old code from throwing.
I'm just returning NaN instead, the same as for invalid mom.

Indeed a behavior change I allowed myself, but only for a private function, which doesn't seem like it's designed to ever throw. I also believe any dev will get and early enough error from the NaNa anyway if that's a concern.

Did I get it right?
Should I still throw something?

return isUTC ? d.getUTCHours() : d.getHours();
case 'Date':
return isUTC ? d.getUTCDate() : d.getDate();
// case 'Day': return isUTC ? d.getUTCDay() : d.getDay(); // Not used
Copy link
Member

Choose a reason for hiding this comment

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

Why is this unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for leaving the comments in. There no problem or anything. It's just that that getSetDayOfWeek has it's own implementation instead of makeGetSet('Day', true) and it doesn't call get(this, 'Days') like getSetMonth does.

This is actually the reason get('day') performance was higher than the rest :)
Anyway, now that wouldn't matter with new code, so I'll make sure get(this, '...') is used where is can be used as part of this commit. That looks like 1 LOC in a couple of files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!
Now using get in getSetDayOfWeek.
I ended up leaving the comments in set() and added the reason we cannot do this for setMonth.
It can be used there too, if I implement separate private functions instead of get and set.
I'd love to do that too, but that's a bigger move and I think should be discussed separately.

- Use it in `getSetDayOfWeek`
- Also update style and comments for commented out `set()` cases
@tomerle
Copy link
Contributor Author

tomerle commented Aug 11, 2022

@marwahaha, @ichernev,
Any chance this is going in? Anything I can do to help?

BTW, this is just the first step (step zero were the extra benchmarks), and I have more impactful improvements waiting. I'd love to see all these improvements go through, following that to define best practices for maintaining them.

@ichernev
Copy link
Contributor

Merged in e64ced7

@ichernev ichernev closed this Dec 24, 2023
ichernev added a commit that referenced this pull request Dec 24, 2023
[misc] Use switch-case for better get-set performance (#2659)
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

4 participants