Back to Articles
Mar 02, 2022
5 min read

It's Time for JS-Joda, but Beware of the Dark Side!

A few days ago, we discussed Moment.js, a JavaScript library for handling dates, and why we should no longer use it. But now what?

Master Yoda surrounded by code.

This article was originally written for Devmy and can be found here along with many other interesting reads. 😉

A few days ago, we discussed Moment.js, a JavaScript library for handling dates, and why we should no longer use it. But now what?

As we mentioned, we have several alternatives available:

Which JS Library Should You Choose?

All the listed solutions are extremely valid: they offer immutable APIs and provide TypeScript definition files (with the exception of Luxon, which relies on @types).

At this point, it all depends on what your project needs: Performance? Integrations? Overall size?

These statistics can help guide your choice:

Performance

The graph below shows the average time these libraries take to perform common date manipulation operations.

A Cartesian plane compares the various libraries in execution milliseconds for functions like parseISO, format YYYY-MM-DD, adding 2 years to a date, month difference, and less-than comparison. JS-Joda nearly touches zero in all listed operations with clear performance superiority, except for parseISO, where it is slightly slower than the others. Another interesting observation is that Day.js takes an excessive amount of time to perform the month difference.

GitHub Stars

  • Day.js: 38k
  • date-fns: 28.1k
  • Luxon: 12.1k
  • JS-Joda: 1.8k

Dependent Packages

  • date-fns: 10k
  • Day.js: 6k
  • Luxon: 2.6k
  • JS-Joda: 68

Open Issues

  • Day.js: 366
  • date-fns: 282
  • Luxon: 90
  • JS-Joda: 14

Size

  • Day.js: 2.8kb (minified, gzipped)
  • date-fns: 19.5kb (minified, gzipped)
  • Luxon: 20.9kb (minified, gzipped)
  • JS-Joda: 39.8kb (minified, gzipped)

Each of these libraries excels at something, but among them, the one that has caught our attention the most is JS-Joda (and not just because its name resembles someone we deeply admire…).

Doesn’t JS-Joda Seem a Bit “Java-ish”?

Perhaps because this utility is the port of the Threeten backport, which is the foundation for the JSR-310 implementation of the Java SE 8 java.time package (while Threeten is inspired by Joda-Time, sharing very similar concepts, if not the same author).

JS-Joda

Handling dates is the path to the Dark Side. Dates lead to anger, anger leads to bugs; bugs lead to suffering. Ah… I sense many bugs in this code.

Considered the greatest Jedi library of its time, JS-Joda is one of the most skilled handlers of dates in the galaxy’s history. Based on the ISO calendar system, it provides us with completely native-independent, simple, and immutable objects. Thanks to these features, it can manipulate dates at will, validate formats influenced by the Dark Side, and format with great vigor.

Where Can You Use JS-Joda?

JS-Joda is a vanilla library with no third-party dependencies and can be included in any JavaScript (or TypeScript) project: this is because Jedi libraries are called to bring peace and justice to all Node stacks in the galaxy, especially those threatened by the Dark Side of dates and context switching.

Why Should You Use JS-Joda?

As already mentioned, the choice of a JavaScript library depends on what your project needs. But here are the reasons why JS-Joda has caught our attention:

  • Fast: JS-Joda is extremely fast. It is 2 to 10 times faster than competing libraries. 🚀

  • Lightweight: The minified and compressed package weighs only 43kb and is free of additional dependencies.

  • TypeScript: JS-Joda provides its own TypeScript definition set, without needing to rely on third-party projects like @types.

  • Standalone: JS-Joda implements its temporal entities independently of the native Date object, unlike libraries such as Moment.js or date-utils, which merely act as wrappers.

  • Immutable: Unlike Moment.js, JS-Joda is immutable: bugs caused by unexpected aliasing were not foreseen in its training.

  • Domain-Driven Design: Its APIs are modeled based on Domain-Driven Design, with classes for each of the different use cases, such as LocalDate, ZonedDateTime, or Period.

  • Robust: This library boasts over 1700 test cases executed via Karma on Firefox, Chrome, Node, and PhantomJS, bringing along test permutations from the Threetenbp project.

Conclusions

Even though it is not at the top of npm’s highest downloads, JS-Joda is an excellent library for date manipulation, with intuitive APIs for the user and extremely fast runtime performance.

A significant advantage (in my humble opinion) is that it derives from Threeten and Joda-Time, inheriting all the experience encapsulated in their code and the vast range of use cases gathered over time by the large Java community.

And let’s face it, it definitely has the coolest name. 😉

Credits

  • Cover image created by Simone Trovato Monastra ❤️
  • Text revisions by Elena Costa ❤️
  • Performance Graph from Best Moment.JS Alternatives by Jakub Kozak.