-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use Set.has instead of Array.indexOf for enum comparison (perf improvement) #2659
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
Use Set.has instead of Array.indexOf for enum comparison (perf improvement) #2659
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
24b8348 to
fd363f3
Compare
|
Thanks this is excellent! I switched to the |
|
This has landed in Zod 3.23. https://github.com/colinhacks/zod/releases/tag/v3.23.0 |
Improve zod's performance by using
Set.hasinstead ofArray.indexOfwhen checking for the presence of a value insideZodEnumorZodNativeEnum.Array.indexOfis especially problematic when passing multiple (e.g. 30+) values to an enum. This PR introduces a very simple change that turns anO(n)operation toO(1).Benchmarks
Before this PR
After this PR
Conclusions
Set.haswe were able to increase the performance of theparsemethod inZodEnumandZodNativeEnumby 20%.Setinstead ofArray.