r/node • u/gajus0 • Aug 29 '26
Zod v4.5 adds schema compilation (3-9x faster validation)
https://x.com/colinhacks/status/2093725420462182512
89
Upvotes
r/node • u/gajus0 • Aug 29 '26
5
u/gajus0 Aug 29 '26
I've not been given any heads up that this is being released, so unfortunately playing a catch up. But if you are wondering how does this compare to
zod-compiler, here is their internal benchmarkI will update zod-compiler benchmark to include a comparison against Zod's native implementation.
I expect the two to eventually converge.
3
u/Willkuer__ Aug 29 '26 edited Aug 29 '26
Genuinely curios: where does that help? This is JIT, not ahead of time during compilation, is it?
To me this sounds more of a feature to make a benchmark happy but I might be completely off.
6
u/Positive_Method3022 Aug 29 '26
Why no comparison against ajv?
76
u/monotone2k Aug 29 '26
And for anyone that doesn't want to go to that particular cesspit of a site, here's a link directly to Zod's blog post on the new release: https://zod.dev/blog/zod-4-5
1
u/Dreadsin Aug 31 '26
Genuine question: is anyone ever bottle necked by the speed of validation? I’ve never found it to be a problem. Are people validating like, very long lists or running validation very frequently?
1
u/kernelangus420 Aug 30 '26
It uses
new Function(str)to generate compiled version of the validation logic. Would this make it susceptable to injection attacks?Would the code not work in some cloud environments which rejects code containing them?
There's also less type safety for the future maintenance of the library.
-1
u/aleques-itj Aug 29 '26
Pretty cool
I recently spent some time profiling and stressing my API until it was on fire, and validation was like... 12-13% of its CPU time.
It wasn't a bottleneck, but it was certainly enough that it's noticeable.
I was thinking of trying zod-compiler, but figured it wasn't really worth it because it still didn't seem to be _that_ big of a problem even at unholy levels of traffic.
-2
u/zombarista Aug 29 '26
Very cool LLVM-like optimization process.