perf: mark functions as @__NO_SIDE_EFFECTS__
#5475
Open
+225
−0
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 without 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.