What is the size impact of importing multiple methods from 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.
No comments yet. Be the first to comment.
In this series, I'll check how various date manipulation libraries perform in the build - especially in regards to output size.
This article will show how to work around a small gotcha in Day.js and show the library's code for the second round of the build size benchmark. Backstory As I explained in the second article about date-fns, comparing the use of only one method skews...
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...

In this article, I'll look at application size with multiple methods imported from date-fns. In the previous benchmark attempt, I was only using 1 method from each library. date-fns, as the only one built with tree-shaking in mind, enjoyed an advantage in this comparison that it will not have in real-world applications.
All project details are as before. You can check the previous article to find out more.
To make a more fair comparison, I want to add more date operations. Such as:
First, let's try all-purpose formatting:
import { sub, startOfQuarter, format, differenceInDays } from "date-fns";
const today = new Date(),
quarterStart = startOfQuarter(today),
diffDays = differenceInDays(today, quarterStart);
console.log("Yesterday was", sub(today, { days: 1 }));
console.log(
"The quarter started on",
format(monthStart, "yyyy-mm-dd"),
"and today, it is",
diffDays,
"days since then"
);
The first downside, the formatting tokens are different than in Moment.js & other libraries. It means we will have to map all the values as we migrate.
The other downside is the build size:
Which is more than what I got in Day.js benchmark.
Ok, we use a library optimized for tree-shaking. Maybe we shouldn't import the most complex method from it. We can make some effort to optimize the format, especially as they provide many methods for it:

import { sub, startOfQuarter, formatISO9075, differenceInDays } from "date-fns";
const today = new Date(),
quarterStart = startOfQuarter(today),
diffDays = differenceInDays(today, quarterStart);
console.log("Yesterday was", sub(today, { days: 1 }));
console.log(
"The quarter started on",
formatISO9075(quarterStart, { representation: "date" }),
"and today, it is",
diffDays,
"days since then"
);
The build size:
This is much better and about half of what adds Day.js.
The examples used here have their own branches - the naive formatting & the final code
It looks like date-fns is indeed the smallest option, as long as we are willing to invest the effort to find alternatives to use the all-purpose format method.