Day.js vs date-fns
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
Search for a command to run...
I'm JS developer with 13 years of professional experience. I'm always happy to teach my craft.
In this series, I'll check how various date manipulation libraries perform in the build - especially in regards to output size.
In this article, I'll take a quick look at the build size of a simple code that imports a method from date-fns. I check results from both, webpack & esbuild. The code The code I use in this test is as follows: import { sub } from "date-fns"; const t...
I’ve been working in programming for the last 16 years. Let’s see what changes I see in our industry that are enabled by generative AI. Rapid prototyping The last few months, I’ve been working on a set of WordPress plugins, related to event organizat...
Writing unit tests takes time and effort. Nonetheless, many teams insist on writing them anyway—that’s because of the benefits they bring to a project. Those benefits are mainly the following: fast feedback—unit tests speed up each iteration of twea...

Pure functions are the perfect case for unit testing. For a given input, we always expect the same output—there is no internal state involved. Let’s take a look at a few examples and some simple tests that check if the methods work as expected. Jasmi...

Let’s say you have a job interview in a few days. How should you prepare for it so that you can make an informed decision about joining the company, as well as make sure that your interests are taken care of? Prepare your questions An interview is a ...

Creating example projects is a common way of showing your skills to potential employers. Let’s take a look at what’s important to keep in mind when building personal projects with an eye toward impressing prospective employers. Simplicity Building ap...

On this page
In this article, I'll summarize Day.js & date-fns comparison, that concludes this series.
Based on what I've learned in the first round of articles, Day.js & date-fns look the most interesting.
In the first test, where I used only one method, the application was at 1.6 KiB in both webpack & esbuild. That was the best result from all tested libraries, but the functional API of date-fns is built for tree-shaking. As this way of testing emphasizes gains of three-shaking, the size advantage of date-fns can diminish if we use more methods.
The second test, with using more methods started above 22 KiB. That's because format is huge. I found a smaller one that can achieve the same result by shopping around among many date formating methods. Then the final results were:
The main advantage of Day.js is that is has the same API as Moment.js. This makes it especially easy if we have Moment used all over the place in our project, and we want to replace is it with something smaller or newer. In my first test, the application was 6.64 KiB when bundled with webpack, and 7.0KiB with esbuild.
To keep the comparison fair, I recreated the second test app. I found a gotcha - Day.js pretend to provide complete API, but some method doesn't work as expected until you add a plugin with a correct implementation. With the plugin in place, the results are:
I would go with date-fns for projects where I'm very build-size-conscious, and I can manage without a fully flexible format function. Day.js is for sure a good migration option for projects that are using Moment.js. The only downside is that we have to pay attention to all the plugins we need to import to ensure all works as expected. The 4KiB difference may not be enough to justify the effort of rewriting all the date manipulation logic.
This article concludes the series. Please let me know if there is some other library I should take a look at.