← All writing

3 min readPerformanceWebpackBuild tools

When code splitting doesn’t help

I split our checkout’s bundle into chunks, measured, and took it out two days later. Code splitting is good advice, but not for every page. Here’s how to tell.

While cutting down our hosted payment page’s bundle, I tried the most common piece of performance advice there is: split the bundle into chunks. Two days later I took it out again. That’s not a failure story. It’s a reminder that “split your bundle” is a rule of thumb, and rules of thumb have edges.

What I tried

The page was a single React app built with webpack, shipping one main script of about 375 KB after the other bundle fixes. Webpack’s splitChunks setting tells it to break shared and third-party code into separate files instead of one big one. I tried it with a 100 KB minimum chunk size first, then 500 KB:

// craco.config.js, roughly what I tried
webpack: {
  configure: (config) => {
    config.optimization.splitChunks = { chunks: 'all', minSize: 100 * 1024 };
    return config;
  },
},

The build produced several smaller files instead of one large one. It didn’t make the page meaningfully better for customers, so I reverted it and kept the simpler single bundle.

Why splitting usually helps

Splitting earns its keep in two situations:

  • Many pages, different code. If your settings screen and your dashboard share little code, a visitor to one shouldn’t download the other. Splitting by route, so each page loads only its own code, is a clear win.
  • Code that changes at different speeds. Libraries change rarely, your app code changes every release. Put them in separate files, and a returning visitor’s browser can keep the library file cached across releases.

Why it can fail to help

A payment page is close to the opposite case:

  • There’s essentially one screen, and it needs nearly everything. Splitting doesn’t remove code the customer needs. It just divides it into pieces they all still download before they can pay.
  • More files means more requests. Each file has its own request and, depending on how scripts load, they can end up waiting on each other. On a slow mobile connection, a few requests in a row can cost more than the bytes you’ve divided.
  • Caching across visits matters less. Plenty of customers see a payment page once, so “cached for next time” saves nothing for them.
  • It adds things to maintain. Chunk sizes, cache groups and the order scripts load in all become settings someone has to understand later.

Splitting a bundle doesn’t make it smaller. It changes when and how the pieces arrive. If nearly every piece is needed up front, the total is what counts, and the way to win is to ship less code.

What I do now

  • Find the waste before you rearrange it. In the same week, removing one 80 KB library did more than any chunk setting could. That story is here.
  • Split by what people use, not by size. Route-based splitting, or lazy-loading a rarely used dialog, beats a size threshold.
  • Measure on the device that matters, then decide. If the numbers don’t move for a customer on a phone, the change isn’t worth its complexity, however good the advice behind it.
  • Reverting is a result. Two days to learn “not here” is a good trade, and it’s worth writing down so nobody tries the same thing next year without the context.
↑↓ move↵ openesc close