r/programming • • Aug 29 '26

Zod v4.5 adds schema compilation (3-9x faster validation)

https://x.com/colinhacks/status/2093725420462182512
173 Upvotes

36 comments sorted by

39

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 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.

9

u/xlzqwerty1 Aug 29 '26

Thanks gajus. Love your work on slonik as well.

3

u/gajus0 Aug 29 '26

I greatly appreciate you for using it and taking time to leave a comment. 🙇

37

u/repeating_bears Aug 29 '26

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?

25

u/gajus0 Aug 29 '26

I have good news for you zod-compiler

9

u/repeating_bears Aug 29 '26

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 

18

u/gajus0 Aug 29 '26 edited Aug 29 '26

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-compiler specifically with backend systems in mind (Slonik, to be specific)

I kinda wish that zod-compiler just 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 Aug 29 '26

"Zod's optimizes for bundle size" 

Does it though? It ships something multiple times the size of zod's core

4

u/gajus0 Aug 29 '26

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-compiler will quite literally inline everything (unless you enable compact mode).

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.

4

u/gabeech Aug 30 '26

One additional thing for client side, since (on a quick glance) it uses new Function(…). That requires an unsafe-eval CSP directive, which is a terrible security issue. And, may not be available to sites that need to pass most info sec audits.

68

u/segv Aug 29 '26

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.

60

u/ur_frnd_the_footnote Aug 29 '26

The compiler explodes the bundle size. Not a dealbreaker for many use cases but enough of a caveat that you probably wouldn’t want it to just suddenly turned on universally 

17

u/blehmann1 Aug 30 '26

Aside from the other comments, it requires unsafe-eval to be set in your content security policy if you use it on the front-end. It compiles a validator by calling the Function constructor with a string argument, so it does use an injection sink that normally you would want to disable.

Might not be the biggest issue for most people, but it is a notable downside. If you care about CSP, that is.

6

u/modernkennnern Aug 31 '26

That's crazy. That makes it a no-go for my company immediately

1

u/br1ghtsid3 Aug 31 '26

Why?

3

u/modernkennnern Aug 31 '26

Disabling arguably the biggest security fix in CSP just to slightly improve performance of I/O is an impossible sell

9

u/Business-Row-478 Aug 30 '26

Assuming it functions similar to ajv, the compilation step takes time and isn’t a trivial operation.

For something like a long-running node process, running once on application startup isn’t a big deal and improves performance overall. But if it’s being used in a serverless compute, the compilation step is run on every single invocation, making performance even worse.

41

u/BadlyCamouflagedKiwi Aug 29 '26

Statically typed languages: Look what they have to do to mimic a fraction of our power...

92

u/Turtvaiz Aug 29 '26 edited Aug 29 '26

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

12

u/fiah84 Aug 29 '26

nah chuck those bits in there raw and let's ball, ain't nobody got time for that

-39

u/BadlyCamouflagedKiwi Aug 29 '26

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 using z.string(), z.number() etc. You could just write that as a struct in Go and decode it using encoding/json and there you go.

46

u/sampullman Aug 29 '26

This isn't a static vs. dynamic thing, it's a standard library vs. third party library thing.

13

u/teerre Aug 29 '26

Uh, what you think encoding/json does?

11

u/Absolute_Enema Aug 29 '26 edited Aug 29 '26

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.

11

u/TheWix Aug 29 '26

This doesn't cover validation

1

u/pakoito Aug 30 '26

This is more a Serde or a Jackson/Gson than a compiler, and neither of those have the type level expressivity of typescript, nor are they known for their fast compile times.

1

u/case-o-nuts Aug 30 '26

Why would anyone still be on Twitter?

-5

u/kiljacken Aug 29 '26

What the heck is a zod?

4

u/mizatt Aug 30 '26

Kneel before zod

-10

u/Fair-Presentation322 Aug 30 '26

The fact that Zod exists shows that we failed as an industry.

5

u/propeller-90 Aug 30 '26

Because...?

3

u/Which-Arm-4616 Aug 30 '26 edited Aug 30 '26

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 Aug 30 '26

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

u/Fair-Presentation322 Aug 31 '26

Hahaha omg yes you put it perfectly

2

u/[deleted] Aug 30 '26

[removed] — view removed comment

1

u/gajus0 Aug 30 '26

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.