Reducing Zod's Memory Footprint by an Order of Magnitude with Method Memoization
Zod
Zod 4.5 cut a plain string schema’s retained heap from 7.5kb to 784 bytes. It does it by memoizing bound methods only when they’re actually used.
Based on reporting by Zod — read the original for the full story.
Summary, retelling and take written by AI under human oversight; images are AI-generated illustrations. How we work · Report an error
Zod 4.5 takes a very specific swing at memory bloat: it stops paying for bound methods up front. The new trick, called method memoization, means a schema method is only turned into a bound closure the first time someone touches it. If nobody ever calls that method, nothing gets allocated for it at all.
That matters because Zod has long auto-bound its schema methods so they don’t depend on implicit this behavior. Handy, yes. But also expensive. Every method became its own bound function, which meant instances could no longer lean on shared prototype methods in the usual way.
The old cost shows up clearly in the benchmark numbers from the repo. A bare z.string() in Zod 4.4 retained 7.5kb of heap; in 4.5 it retains 784 bytes. Similar drops show up across other schema types too, from z.number() at 4.4kb down to 706b, and z.object({...}), 10 keys from 82.0kb to 11.0kb. The pattern also lands in zod/mini, where a string schema drops from 2.5kb to 577b.
The implementation is neat. Zod classes define methods as getters on the prototype. The first access falls through the normal prototype chain, the getter binds the method to that instance, then stores it on the instance as its own property. After that, the method is fetched directly, so the one-time cost is paid once and then memoized forever.
There’s a second win hiding underneath the first. V8 stores fewer than 13 own properties in a compact 128-byte backing store, then jumps to 848 bytes at 13 or more, and to 1616 bytes at 21 or more. A regular string schema in [email protected] had 49 own properties, so every instance got the 1616-byte store. Zod 4.5 keeps only six eager own properties — _zod, def, type, format, minLength, and maxLength — and pushes the rest onto the memoizing prototype.
My take — AI-written commentary, not fact-checked reporting
This is the kind of boring engineering that actually deserves applause: fewer magic methods, fewer wasted bytes, fewer little tax stamps on every object. The industry loves to talk about elegant APIs while quietly letting helper code balloon into furniture. Zod just made the furniture disappear unless someone sits on it.
Read more about this at: Zod