feat(v4): add explicit z.precompile(...) for startup-restricted runtimes#5728
Open
whoiskatrin wants to merge 1 commit intocolinhacks:mainfrom
Open
feat(v4): add explicit z.precompile(...) for startup-restricted runtimes#5728whoiskatrin wants to merge 1 commit intocolinhacks:mainfrom
whoiskatrin wants to merge 1 commit intocolinhacks:mainfrom
Conversation
f63956d to
24f2564
Compare
Author
|
Just to be specific this is not “automatic JIT for all Workers code” and not guaranteed speedup for dynamically created per-request schemas. Best value comes when schemas are module-scope and reused. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an explicit
z.precompile(...)API to precompile object fast-paths in environments wherenew Functionmay only be available during startup (for example Cloudflare Workers).Instead of relying on implicit runtime heuristics/timing, this makes the behavior deterministic and opt-in.
Prior context
This builds on earlier exploration in #5462 and #5565. The approach here favors an explicit precompile API over implicit runtime heuristics/timing so behavior is deterministic and easier to reason about in startup-restricted runtimes.
What changed
precompile(schema | schema[])in core:packages/zod/src/v4/core/precompile.tspackages/zod/src/v4/core/index.tspackages/zod/src/v4/classic/external.ts(z.precompile(...))packages/zod/src/v4/core/schemas.tspackages/zod/src/v4/core/util.tsnew Function) without hardcoded Cloudflare user-agent disabling.packages/docs/content/v4/index.mdxAPI
z.precompile(...)is best-effort:Test approach
New focused tests
Added
packages/zod/src/v4/classic/tests/workers-jit.test.tswith coverage for:union,record,optional,readonly,catch,lazy).Functionto throw and verifies object parse still succeeds via non-JIT path.Regression checks run locally
pnpm test packages/zod/src/v4/classic/tests/workers-jit.test.tspnpm test packages/zod/src/v4/classic/tests/object.test.ts packages/zod/src/v4/classic/tests/lazy.test.ts packages/zod/src/v4/classic/tests/record.test.tspnpm test) successfully.Notes