~/gvndsxnkr
cd ../notes

// performance · node.js · 4 min read

A Date Formatter That Ate 2.6 GB of Memory

Creating Intl.DateTimeFormat in a hot loop pushed native memory past the host's limit; reusing one cut a request from 4.8 s to 0.28 s.

An operations app I built for an agri-processing centre kept getting killed by its host. No crash, no stack trace: the platform simply restarted the instance for exceeding its memory limit. It always happened around the same screen, the analytics dashboard.

The cause turned out to be a single line that formatted dates.

The symptom

One analytics request could grow the Node.js process by about 200 MB. Asking for a long date range pushed it to 1.7 GB. Under a load test the process climbed past 2.6 GB and didn't come back down. Garbage collection ran and the heap looked reasonable, but resident memory kept rising.

That gap is the clue: when the JavaScript heap is fine but the process keeps growing, the memory is being allocated outside the heap, in native code.

The line

The backend groups processing batches by day in the centre's timezone. A small helper produced today's date as YYYY-MM-DD:

export function todayYmd(now = new Date()) {
  // en-CA formats as YYYY-MM-DD.
  return new Intl.DateTimeFormat("en-CA", {
    timeZone: process.env.APP_TIMEZONE || undefined,
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
  }).format(now);
}

Nothing is wrong with it in isolation. The problem was how often it ran: analytics called it for every batch in every chart period, hundreds of thousands of times per request.

Each new Intl.DateTimeFormat(...) builds a formatter backed by ICU, the native internationalisation library inside V8: locale data, timezone rules, pattern caches. That memory lives outside the JavaScript heap. In principle it's freed once the formatter is garbage-collected. In practice, on our Node version and host, creating formatters this fast pushed native memory up much faster than it came back, and the instance hit its limit first. I didn't dig into exactly where it was held. What mattered was that formatter creation was far from free, and that we were doing it hundreds of thousands of times per request.

The fix

Build the formatter once and reuse it. The timezone comes from configuration, so the cache is keyed on it:

let cached: { zone: string | undefined; format: Intl.DateTimeFormat } | null = null;

function ymdFormatter(): Intl.DateTimeFormat {
  const zone = process.env.APP_TIMEZONE || undefined;
  if (!cached || cached.zone !== zone) {
    cached = {
      zone,
      format: new Intl.DateTimeFormat("en-CA", { timeZone: zone, year: "numeric", month: "2-digit", day: "2-digit" }),
    };
  }
  return cached.format;
}

export function todayYmd(now = new Date()) {
  return ymdFormatter().format(now);
}

A single slot is enough because there's one timezone per deployment. If the timezone ever becomes per tenant or per request, this should become a Map keyed by timezone.

The result

That was the only change in the fix, measured the same way before and after:

MeasureBeforeAfter
Memory under a 900-request load testclimbing past 2.6 GBflat at about 415 MB
All-time analytics request4.8 s0.28 s

The speed-up was a bonus: building a formatter is expensive, so doing it hundreds of thousands of times was also most of the request's CPU time.

What I'd do next

Reusing the formatter removes the allocation cost, but the loop still formats dates it has already formatted. The better structure is to format each distinct day once: group the batches by a numeric day key first, then turn only the unique keys into strings. That makes the hot loop independent of date formatting entirely.

What I took away

  • Intl objects are expensive to construct and cheap to reuse. The same goes for Intl.NumberFormat, Intl.Collator and friends. Treat them like compiled regular expressions: create once, call many times.
  • Watch resident memory, not just the heap. A healthy heap with a growing process points at native allocations.
  • Load-test the heavy screens. One click on the dashboard looked fine. Nine hundred requests told the real story in minutes.
  • #Performance
  • #Node.js