-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
perf: lazy initialize ~standard schema property #5363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
perf: lazy initialize ~standard schema property #5363
Conversation
|
Great idea. I appreciate you looking into the initialization perf. How are you benchmarking that? I'd like to have a decent benchmark for initialization if you have one - feel free to add it under General approach is good. This same pattern is already implemented as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes schema initialization performance by converting the ~standard property from eager initialization to lazy loading with caching. The Standard Schema protocol support object is now only created when accessed, reducing unnecessary overhead for applications that don't use this feature.
Key Changes:
- Converted
~standardproperty to a lazy getter usingObject.defineProperty - Added caching mechanism to store the result after first access
- Reformatted the async fallback in the validate function for better readability
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughThe $ZodType class's "~standard" property initialization was refactored to use lazy initialization via util.defineLazy. Previously, the property was initialized directly and eagerly. Now, the object creation is deferred until the property is first accessed. The change maintains the same validate function, vendor, and version metadata. The public interface and external behavior remain unchanged; only the internal initialization approach was modified from eager to deferred execution. Pre-merge checks✅ Passed checks (3 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (11)**/*.{js,jsx,ts,tsx,mjs,cjs,json}📄 CodeRabbit inference engine (CLAUDE.md)
Files:
**/*.{js,jsx,ts,tsx,mjs,cjs}📄 CodeRabbit inference engine (CLAUDE.md)
Files:
**/*.{ts,tsx}📄 CodeRabbit inference engine (CLAUDE.md)
Files:
**/*.{ts,tsx,js,jsx,mjs,cjs}📄 CodeRabbit inference engine (CLAUDE.md)
Files:
**/*.{js,mjs,ts,tsx}📄 CodeRabbit inference engine (.cursor/rules/development-setup.mdc)
Files:
**/*.{js,mjs,cjs,jsx,ts,tsx}📄 CodeRabbit inference engine (.cursor/rules/guidelines.mdc)
Files:
packages/zod/src/v4/core/{schemas.ts,core.ts}📄 CodeRabbit inference engine (.cursor/rules/zod-internals.mdc)
Files:
packages/zod/src/v4/core/schemas.ts📄 CodeRabbit inference engine (.cursor/rules/zod-internals.mdc)
Files:
packages/zod/src/v4/core/{schemas.ts,checks.ts}📄 CodeRabbit inference engine (.cursor/rules/zod-internals.mdc)
Files:
packages/**/src/**/*.{ts,tsx}📄 CodeRabbit inference engine (.cursor/rules/zod-project-guide.mdc)
Files:
packages/zod/**📄 CodeRabbit inference engine (.cursor/rules/zod-project-guide.mdc)
Files:
🧠 Learnings (15)📓 Common learnings📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:26:08.288ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:26:08.288ZApplied to files:
📚 Learning: 2025-10-21T17:28:01.210ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:27:32.492ZApplied to files:
📚 Learning: 2025-10-21T17:24:39.708ZApplied to files:
📚 Learning: 2025-10-21T17:24:39.708ZApplied to files:
📚 Learning: 2025-10-21T17:24:39.708ZApplied to files:
🔇 Additional comments (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This PR improves schema initialization performance by (~5-15%) by deferring the creation of the "~standard" property until it's actually accessed.
Problem
The ~standard property implements the https://github.com/standard-schema/standard-schema but is rarely used in typical applications. Currently, this object is created eagerly for every schema during initialization, adding unnecessary overhead:
Solution
Convert the property to a lazy getter that only creates the object when accessed, then caches the result: