Skip to content

feat: create cache method for caching schema output by input #1170

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

EskiMojo14
Copy link
Contributor

fixes #923

const cachedSchema = v.cache(baseSchema, { maxSize: 100, duration: 5_000 })

Copy link

vercel bot commented Apr 25, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valibot ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 1, 2025 3:52pm

Copy link
Owner

@fabian-hiller fabian-hiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for creating this PR. I will probably plan it for v1.2 or v1.3.

private duration: number | undefined;
private cache = new Map<TKey, _CacheEntry<TValue>>();
constructor(options?: _CacheOptions) {
this.maxSize = options?.maxSize ?? 1;
Copy link
Owner

@fabian-hiller fabian-hiller Apr 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should maxSize be Infinity or a number like 100 or 1,000 if not set? Infinity is probably a bad default because attackers could abuse it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a default of 1 is pretty common - for example reselect and proxy-memoize

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But doesn't this mean that cache is pretty useless if you don't specify maxSize?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. If 2nd run is done with same value, it wouldn’t re-evaluate, just use cached result

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open to discussing this, but right now I would probably choose a default that makes the most sense, so that people only need to define a custom value for special needs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for memoize libraries i think a lot of people leave it as 1, but i guess it could be argued a schema is more likely to be called with different values

Copy link

pkg-pr-new bot commented Apr 27, 2025

Open in StackBlitz

npm i https://pkg.pr.new/valibot@1170

commit: beb3c8b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: memoize result of asynchronous schema
3 participants