MomentJS has been the go-to library for many JS developers for quite some time for date formatting, parsing, and manipulating. It comes with very good features but also comes with some limitations. An excellent alternative to MomentJS is DayJS, a minimal library at just 2kB!
I’ve used MomentJS in many of my old react and angular projects and I still like it. The size of MomentJS used to be 232 kB, 66 kB gzipped (Old version) which was quite big for any project so I started looking for alternatives.
I’ve used few other libraries like JS-Joda as well but I found DayJS to be very convenient with a simple API similar to Moment and just 2kB size
Let’s compare DayJS and MomentJS
The latest version of MomentJS as of June 2020 has a size of 58kB minified and 18kB gzipped
MomentJS with all locales has a size of 350kB minfied and 72kB gzipped
The size of MomentJS without locales seems to be around 200kB on ES6 imports as mentioned by few users on MomentJS size issue on their Github repo.
The Latest version of DayJS as of June 2020 has a size of 7kB minified and 2kB Gzipped.
DayJS with all locales has a size of 167kB Minified and 37kB gzipped
Performance, DayJS VS MomentJS
I ran 2 basic tests on both the libraries.
- Parse an ISO date and format it
- Parse an ISO date and get seconds from it
Here are the results:
1st test :
MomentJS took 1.6949999990174547 milliseconds
DayJS took 0.3600000054575503 milliseconds
2nd test :
MomentJS took 0.6649999995715916 milliseconds
DayJS took 0.22000000171829015 milliseconds
As you can see, DayJS performs 3 to 5 times better!
Considering the size and performance, It is clear that DayJS is an excellent alternative to MomentJS. Go for it!
This article is written in no way to degrade MomentJS. They have done some great work and thousands of developers have benefitted from the library. It’s just that DayJS is better at this point in time.
MomentJS has improved a lot recently. We never know MomentJS might improve further and become the best. So watch out!
- Curved button using Flutter | Can it be done with Flutter? | Custom shapes Flutter | Custom painter - September 21, 2022
- Dynamic Island using Flutter | iPhone 14 Pro | Can it be done with Flutter? - September 14, 2022
- Best practices of scraping website data for beginners (Updated 2022) - September 12, 2022
Just 2 tests? Moreover, for objectivity, you need to compare performance over a large number of iterations.