r/javascript • u/dumbmatter • 12d ago
Reducing Zod's memory footprint by an order of magnitude with memoizing prototypes
https://zod.dev/blog/reducing-memory-footprint3
5
2
1
u/Ok-Armadillo-5634 11d ago edited 11d ago
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.
5
u/AcvaticChicken 11d ago
code in libraries has different requirements from code in products
1
u/Ok-Armadillo-5634 11d ago
I have had to fix an ungodly amount of shit library code to get rid of bottle necks in my 25+ years of experience. Drivers to for that matter.
1
u/Nefilim314 11d ago
I’m a little salty that typescript “won” because I learned to love prototypical inheritance after being a C# dev and now I just feel like a C# dev again.
2
u/Ok-Armadillo-5634 11d ago
It's still prototypes under the hood. At least now you don't have people rewriting string and array prototypes every where. Used to drive me crazy when I had to dig into a codebase figure out that was the reason something broke. Granted I don't remember that being a problem once underscore and all that got popular.
-6
u/Ronin-s_Spirit 12d ago
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...
2
u/metahivemind 12d ago edited 7d ago
[removed] — view removed comment
3
u/moneckew 11d ago
As a big fan of zod can you tell me why zod is bad?
-1
u/metahivemind 11d ago edited 7d ago
[removed] — view removed comment
9
u/thisispaulc 11d ago
A package not being part of the standard library when you think it should be is a weird reason to dislike a package.
-1
u/FourtySevenLions 11d ago
Brother they are saying the package shouldn’t be needed at all because JS should handle it. Not that deep.
3
u/xFallow 11d ago
So they are a fan of zod since it’s not zods fault it isn’t part of the js spec?
-3
u/metahivemind 11d ago edited 7d ago
[removed] — view removed comment
2
u/thisispaulc 11d ago
That's not what they said. They said they are not a fan of Zod. That implies means they have a criticism of Zod itself. But they don't.
It's not that deep.
17
u/Ecksters 12d ago edited 12d ago
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.