You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sync-when-possible parsing: returns a value synchronously when the
schema is sync, a Promise only when an async refinement/transform is
hit.
Implementation:
- Run sync first (ctx.async = false) so ZodObject's JIT fastpass
engages on sync-only schemas; fall back to async on $ZodAsyncError.
- Cache async-observed schemas in a WeakSet so subsequent calls skip
the sync attempt and avoid the throw cost on async-capable schemas.
- ~standard.validate routed through safeParseMaybeAsync (replaces the
previous safeParse + try/catch + safeParseAsync fallback).
Caveat — side-effect doubling on the async fallback path: sync work
running before the first async step executes once during the sync
attempt and again during the async run. Documented in JSDoc; users
should prefer parseAsync for non-idempotent sync transforms.
Bench (packages/bench/object-maybe-async.ts):
- flat sync object: parity with safeParse.
- nested + discriminated sync: parity with safeParse (fastpass engages
end-to-end).
- mostly-sync + 1 async refine: ~1.4x of safeParseAsync after the
first call (down from ~14x without the WeakSet cache).
/** Sync when possible; Promise on async refinement. Sync transforms run twice if the schema then hits an async step — use `parseAsync` if non-idempotent. */
/** Sync when possible; Promise on async refinement. Sync transforms run twice if the schema then hits an async step — use `parseAsync` if non-idempotent. */
0 commit comments