r/programming • u/gajus0 • 3d ago
Zod v4.5 adds schema compilation (3-9x faster validation)
https://x.com/colinhacks/status/209372542046218251240
u/gajus0 3d ago
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 benchmark
zod-compiler is faster on every schema both compile: median 1.75x over
z.compile(), range 1.11x–24x (valid input,safeParse, result consumed;z.compile()itself is a median 3.35x over the runtime on this set, zod-compiler 7.0x). One process per schema shrinks every ratio the way the matrix section describes —z.compile()2.63x, zod-compiler 4.99x, the gap between them 1.49x — without changing the ordering of a single row. [..]
I will update zod-compiler benchmark to include a comparison against Zod's native implementation.
I expect the two to eventually converge.
7
36
u/repeating_bears 3d ago
Cool but implemented wrong.
This should be a build plugin (vite and whatever else). Shipping a 7k compiler to the client just for the philosophical purity of "doesn't require a build step" isn't a good tradeoff
How many serious projects don't have a build step already?
24
u/gajus0 3d ago
I have good news for you zod-compiler
9
u/repeating_bears 3d ago
Nice, didn't know about it. Although with zod's change, the name is going to create confusion (not your fault)
Why didn't zod just adopt this as an official project, assuming you would have wanted that?
Other than being "official" I see no reason to use the thing that's been announced over this
19
u/gajus0 3d ago edited 3d ago
We developed both solutions in parallel without being aware of each other's efforts. Colin wanted to do Zod AOT at least since January. Then we both made public our first explorations days apart.
They are different and take different tradeoffs.
- Zod's is focused on runtime compilation.
- zod-compiler is focused on build time (although runtime is possible).
- Zod's optimizes for bundle size.
- zod-compiler optimizes for maximum speed (although there is compact opt-in).
- Zod's optimizes for maximum compatibility with Zod
- zod-compiler has minor divergence
I don't want to misrepresent Colin's view, but those are my first impressions.
I developed
zod-compilerspecifically with backend systems in mind (Slonik, to be specific)I kinda wish that
zod-compilerjust became part of Zod and we collaborated or vice versa, but either way, I can see space for both projects to coexist.4
u/repeating_bears 3d ago
"Zod's optimizes for bundle size"
Does it though? It ships something multiple times the size of zod's core
4
u/gajus0 3d ago
I only glanced through their implementation, but it looked to me like they are still referencing and re-use code from the core lib. Whereas
zod-compilerwill quite literally inline everything (unless you enablecompactmode).In practice though, since the code is highly repetitive and colocated, most of it disappears thanks to compression (if that's the concern).
Also, I cannot think of many good use cases for Zod compilation client-side. We found AOT incredibly beneficial in data heavy pathways (like SQL query response validation), but I cannot think of many places where this would matter in a client-side app.
37
u/BadlyCamouflagedKiwi 3d ago
Statically typed languages: Look what they have to do to mimic a fraction of our power...
88
u/Turtvaiz 3d ago edited 3d ago
What do you mean? Statically typed languages still need to parse things to be certain that the contents actually are what the type says they are
-41
u/BadlyCamouflagedKiwi 3d ago
Yes, of course, but they don't need to import a library to do that. The example in the post builds a type using
z.object, with fields in it usingz.string(),z.number()etc. You could just write that as a struct in Go and decode it usingencoding/jsonand there you go.44
u/sampullman 3d ago
This isn't a static vs. dynamic thing, it's a standard library vs. third party library thing.
9
u/Absolute_Enema 3d ago edited 3d ago
Guess what, even if it happened to parse you still need to validate it, so you're none the better off.
FWIW zod also integrates with typescript, so you can write the validator and have it give back a static type on the side as well.
2
u/tomveber 2d ago
Honest question for everyone excited about the 3-9x, has schema validation ever actually shown up in your profiler? Every time I've gone looking, the cost was the round trip and the json.parse before it. The one case in this thread where it clearly pays (big sql result sets) is also the one where you already control the build step.
1
u/gajus0 2d ago
As an example, we validate every SQL query result runtime.
https://github.com/gajus/slonik#runtime-validation
And before AOT, Zod accounted for a significant portion of our CPU time.
Same is true for any app that deals with a lot of IO and requires runtime validation.
1
-5
-11
u/Fair-Presentation322 3d ago
The fact that Zod exists shows that we failed as an industry.
4
u/propeller-90 2d ago
Because...?
3
u/Which-Arm-4616 2d ago edited 2d ago
It's a cludge on a cludge, an awkward solution to a problem that shouldn't exist in the first place.
4
u/Which-Arm-4616 2d ago
It does evoke a sense of some remote tribe inventing their own strange and esoteric solutions to long-solved problems in the rest of the developed world.
2
64
u/segv 3d ago
Is there any particular reason why this compilation is not turned on by default? Needing a "magic" second import in every file with a schema is rather.. inconvenient.