perf: mark functions as @__NO_SIDE_EFFECTS__#5475
Merged
colinhacks merged 3 commits intocolinhacks:mainfrom Dec 30, 2025
Merged
perf: mark functions as @__NO_SIDE_EFFECTS__#5475colinhacks merged 3 commits intocolinhacks:mainfrom
@__NO_SIDE_EFFECTS__#5475colinhacks merged 3 commits intocolinhacks:mainfrom
Conversation
Owner
|
cc @pullfrog review |
Contributor
There was a problem hiding this comment.
Solid implementation. The annotations are correctly applied to side-effect-free schema constructors, following the spec and established patterns from Valibot. This will enable better tree-shaking for users.
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.
This PR adds
@__NO_SIDE_EFFECTS__notation for all the schema functions: https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/no-side-effects-notation-spec.mdThis would improve tree-shaking of Zod (mini & core) on users side. For example, the snippet from the docs:
Because the
object()can not be treated as side-effects free, evenLoginSchemais not used, the final bundle still contains an orphanobject()call with it's all dependencies.One solution is to add
/* @__PURE__ */on the user side,The downside is this would require the user's manual effort on every schema.
With
@__NO_SIDE_EFFECTS__, it will give a hint to the bundler to knowobject()is side-effects free, and eventually make the whole file shakable (bundle 0KB)Example of bundling using Rollup: https://stackblitz.com/edit/node-4tux9gdr
This PR made from the open-circle/valibot#995 that @antfu and I co-authored.
Chain calls tree-shaking issue
Since
ZodMiniTypestill includes some chain calls with side effects (such ascheck,brand, etc.), and because chained calls can't currently be optimized through tree-shake annotations, I'll open another PR to introduce APIs likez.checkFor(z.xxxxx(), ...checks)to address these issues.