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

Research Alternatives to Moment JS #1937

Closed
tn3rb opened this issue Dec 2, 2019 · 13 comments
Closed

Research Alternatives to Moment JS #1937

tn3rb opened this issue Dec 2, 2019 · 13 comments

Comments

@tn3rb
Copy link
Member

tn3rb commented Dec 2, 2019

Need to know the various Pros & Cons of various Date libraries

example:

Library Pros Cons
Moment JS great browser support older library, does not support tree shaking or other modern JS capabilities

Possible Alternatives

  • Luxon
  • Date-Fns
  • ???

plz try not to spend more than 4 hours on this

@nicolad
Copy link
Contributor

nicolad commented Dec 9, 2019

Library stars created Tree-shaking Pattern TS support I18n Timezone support
Moment JS 42930 Mar 1, 2011 no OO yes yes Good(moment-timezone)
Luxon 8846 Nov 30, 2015 no OO not yet, there is a WIP Luxon uses the native Intl API Good(Intl)
date-fns 20599 Oct 6, 2014 yes FP yes 67 locales available yes
dayjs 24564 Apr 10, 2018 no OO yes yes need more info

@nicolad
Copy link
Contributor

nicolad commented Dec 9, 2019

@tn3rb @manzoorwanijk @raphaelkross take a look here ☝️

@nicolad
Copy link
Contributor

nicolad commented Dec 10, 2019

Library pros cons
Moment JS - uses chaining
- Moment.js is mutable and it causes bugs
- Complex OOP API (which doubles mutability problem).
- Moment.js has a huge performance overhead because of complex API
- It is highly based on OOP APIs, which makes it fail to work with tree-shaking, thus leading to a huge bundle size and performance issues.

- comparison with date-fns
Luxon - Make all the types immutable.
- Use the Intl API to provide internationalization.
- Luxon has the best time zone support of any JS date library.
- Explicit API: Function names describe exactly what they do so it's easier to work with.
- uses chaining
- uses OOP
date-fns
- works with the native date type
- Functionality-wise date-fns equals to Moment.js.
- All of date-fns's code are pure functions (and simple to grok)
- Locale and internationalization as configs to display functions
- When comparing a certain date to the current date the function returns a pretty "opinionated" sentence such as "almost 2 years" which can't really be customizable and requires string manipulation to fit result to your needs.
dayjs - Similar API as Moment.js

Related links

https://blog.bitsrc.io/9-javascript-date-time-libraries-for-2018-12d82f37872d
https://inventi.studio/en/blog/why-you-shouldnt-use-moment-js
https://blog.logrocket.com/4-alternatives-to-moment-js-for-internationalizing-dates/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat

@nicolad
Copy link
Contributor

nicolad commented Dec 10, 2019

@tn3rb @manzoorwanijk @raphaelkross


The floor is open to comments/ thoughts

@tn3rb
Copy link
Member Author

tn3rb commented Dec 10, 2019

well based on the above, date-fns looks like the clear winner.

re:

When comparing a certain date to the current date the function returns a pretty "opinionated" sentence such as "almost 2 years" which can't really be customizable and requires string manipulation to fit result to your needs.

which function is that referring to?
are there other comparison functions that can be used?

@nicolad
Copy link
Contributor

nicolad commented Dec 10, 2019

well based on the above, date-fns looks like the clear winner.

re:

When comparing a certain date to the current date the function returns a pretty "opinionated" sentence such as "almost 2 years" which can't really be customizable and requires string manipulation to fit result to your needs.

which function is that referring to?
are there other comparison functions that can be used?

It's formatDistance function, but we might want to have our own utility function instead and that can use another lib(maybe).

@nicolad
Copy link
Contributor

nicolad commented Dec 10, 2019

are there other comparison functions that can be used? - I don't know yet/ didn't investigated a separate function/lib/package, but if we'd go with date-fns we are can pick whatever solution we want since it's using Date object type as input and output, so we are not locked into some specific library object type, like in case of moment or other.

@manzoorwanijk
Copy link
Contributor

@nicolad great details dude! 🥇

@manzoorwanijk
Copy link
Contributor

I also agree that date-fns looks to be the best.

@nicolad
Copy link
Contributor

nicolad commented Dec 12, 2019

Ok, I also agree with date-fns.
@tn3rb @manzoorwanijk we need to think of a pathway because we don't want to use the same approach as with old datetime JS model.


Here is one high-level example: https://github.com/palantir/blueprint/tree/develop/packages/datetime/src/common
we can have smth similar, but without moment

@tn3rb
Copy link
Member Author

tn3rb commented Dec 13, 2019

that package isn't using moment...
or did you just mean we can do something like but not use moment like the EDTR v1 Date VOs ?

@nicolad
Copy link
Contributor

nicolad commented Dec 13, 2019

or did you just mean we can do something like but not use moment like the EDTR v1 Date VOs ?

yes, ezactly this ☝️

@tn3rb
Copy link
Member Author

tn3rb commented Dec 13, 2019

I had a good look through the date-fns docs and I think they have everything we need already covered.
That said, we'll still need to build some predicates that will operate directly on our entity types

ie: sortTicketsByStartDate()

these can be composed from smaller building blocks located in assets/prototype/domain/shared/predicates

Going to close this now because we are ready to move on to the next step: #2041

@tn3rb tn3rb closed this as completed Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants