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.
So, zod has a
typeCache
internally that looks at every single value it passes and stores it in a map with its derived type. My assertion is that this method is mostly overhead and in the majority of cases makes things slower, not faster.Map#get
andMap#set
is faster than runningtypeof
. I think in most JavaScript engines, typeof is actually faster than a Map lookup & set.I would like to try and make a better 'real world' benchmark because the current benchmarks are so 'micro' and use the same data - I think that the bench improvement here is demonstrating how typeCache makes zod slower on first pass, but there's more to be said about how these features interact when you're dealing with an array of objects of strings, in which case you have a lot of
._parse
calls.Okay, so before & after:
After: