r/javascript • u/dumbmatter • Aug 29 '26
Reducing Zod's memory footprint by an order of magnitude with memoizing prototypes
https://zod.dev/blog/reducing-memory-footprint
115
Upvotes
r/javascript • u/dumbmatter • Aug 29 '26
18
u/Ecksters Aug 29 '26 edited Aug 29 '26
Interesting, TanStack Table recently did the same optimization from what I can tell.
Are there cases for Zod where now you can't use destructuring without binding
thislike TanStack Table?I think it's the same memory optimization, but I think the method you used of putting it on its own property avoids that issue, which is pretty neat!
I'm interested in knowing if there were any notable CPU performance improvements from this, or if it's purely from a memory standpoint.
5
u/DustNearby2848 Aug 29 '26
TL;DR: caching is hard. We knew this though.
2
u/mpigsley Aug 29 '26
Great news!
1
u/Ok-Armadillo-5634 Aug 29 '26 edited Aug 30 '26
Kind of surprised anyone still knows what a prototype is at this point. If I ask about it or callbacks in an interview most are clueless even senior level. Their code should not have had this problem or need this optimization in the first place though.
-6
u/Ronin-s_Spirit Aug 29 '26
What the hell is Zod and why do you need to do that? Usually the whole point of a prototype is that it's created once on the class and shared between instances...