Skip to content
Development

The 200KB decode nobody needed to redo

By Victor Da Luz
astroperformancetestingdev-logastro-tools

My OG-card generator bundles a fallback font so consumers don’t have to supply their own. It’s stored as a base64 string baked into the package, since satori’s font parser can’t read variable fonts and this sidesteps that entirely by shipping a static weight. Every time the library generated a card, it decoded that base64 string back into raw bytes. Every single time. Even when generating fifty cards in the same build, for fifty different blog posts, using the identical font.

The fix is one if statement

let cachedFonts: SatoriFont[] | null = null;

export async function defaultFonts(): Promise<SatoriFont[]> {
  if (!cachedFonts) {
    cachedFonts = [
      { name: 'Space Mono', data: Buffer.from(regularBase64, 'base64'), weight: 400, style: 'normal' },
      { name: 'Space Mono', data: Buffer.from(boldBase64, 'base64'), weight: 700, style: 'normal' },
    ];
  }
  return cachedFonts;
}

That’s the entire change. Decode once, cache the array, hand back the same reference every time after. The kind of fix that barely feels worth a changelog line.

The test that actually matters here

The easy version of this test calls defaultFonts() and checks the fonts look right: name, weight, that the data is a real Buffer with real content. Fine test, but it would pass identically whether or not the memoization worked, because it only ever calls the function once.

The test that proves anything calls it twice and checks identity, not equality:

test('defaultFonts memoizes - repeat calls return the identical buffer instances', async () => {
  const first = await defaultFonts();
  const second = await defaultFonts();
  assert.equal(first, second, 'should return the same cached array, not a fresh one');
  assert.equal(first[0].data, second[0].data, 'should return the same buffer instance');
});

assert.equal on two Buffers checks reference identity here, not byte-for-byte content equality the way assert.deepEqual would. Two freshly-decoded buffers with identical bytes would fail this test, and that’s exactly the point. The whole bug was “decodes the same bytes twice,” so a content-equality check would happily pass on the broken version too. The only test that can distinguish “memoized” from “not memoized, but consistent” is one asserting that both calls literally handed back the same object.

Why this one was worth doing

It’s a small fix for a small problem. A single build decoding a couple hundred KB of base64 an extra few dozen times isn’t going to make or break anyone’s day. But it compounds invisibly: a site with a hundred posts generating a card per post pays this cost a hundred times over for zero benefit, and nothing about the symptom (a slightly slower build) points a finger at the font decode. Cheap to fix, easy to miss, exactly the profile of bug worth clearing out of a backlog even when nothing is complaining yet.

Related reading