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

Performance bottleneck creating a range using a start and end date #236

Closed
dteirney opened this issue Mar 27, 2018 · 2 comments
Closed

Performance bottleneck creating a range using a start and end date #236

dteirney opened this issue Mar 27, 2018 · 2 comments
Milestone

Comments

@dteirney
Copy link

We've run into a performance challenge using moment-range when creating a range using a start and end date.

We've tracked the challenge down to https://github.com/rotaready/moment-range/blob/master/lib/moment-range.js#L335

With the current implementation:

  • hasOwnProperty is always called regardless of the type of start
  • hasOwnProperty expects a string, so start always has toString() evaluated.
  • for moment dates, toString() is nontrivial and chews through lots of CPU cycles
  • toString() returns a string that will never be found as a property in the internal INTERVALS object (since it's a formatted date and not what is expected, e.g. 'year', 'month', 'day')

If using hasOwnProperty is indeed the best way to hold the valid set of strings for the code path in that if block, then adding a typeof start === "string" check would avoid all that toString() date formatting work.

@gf3
Copy link
Contributor

gf3 commented Mar 27, 2018

great call! thanks for pointing this out. maybe we can sneak a fix into the 4.0.0 release

@TristanJM
Copy link
Contributor

Thanks for this suggestion 😃

Created PR:
#243

Feel free to double check my benchmarks!

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

No branches or pull requests

3 participants