How to get JavaScript bundle size right
The version of this that works is simpler than the version most people imagine. The short answer to JavaScript bundle size is that it is mostly a sequence of small decisions, not one big one.
Code gets read far more often than it gets written, and usually by someone with less context than the author had. Anything you cannot measure here, you are deciding by taste, which is fine as long as everyone knows it.
Why this earns attention
Every kilobyte of JavaScript costs more than a kilobyte of image. This is the sort of thing that compounds, quietly, in both directions. It is worth deciding this deliberately rather than inheriting whatever the last person set up.
The steps
- Establish what you have today before changing anything
- Most sites ship libraries for features they barely use
- Measure the bundle before optimising anything else
- Write down the decision so the next person does not re-litigate it
Measure the bundle before optimising anything else. Small and consistent beats large and occasional here. It rarely shows up as a line item, which is exactly why it slips.
What to do next
The short version
Most development decisions are really maintenance decisions wearing a different hat. Three things worth confirming about JavaScript bundle size before you move on:
- Someone can say what the current setup is without going to look
- Every kilobyte of JavaScript costs more than a kilobyte of image — and you know whether that is true here
- There is a way to tell whether the last change to this helped
The point is not perfection, it is knowing which of these you have consciously chosen to skip.