Skip to content

Fast unions#1026

Merged
colinhacks merged 2 commits into
colinhacks:masterfrom
tmcw:fast-unions
Mar 18, 2022
Merged

Fast unions#1026
colinhacks merged 2 commits into
colinhacks:masterfrom
tmcw:fast-unions

Conversation

@tmcw

@tmcw tmcw commented Mar 17, 2022

Copy link
Copy Markdown
Contributor

Okay, one more for fun I guess.

Right now Zod's unions work by evaluating all options and returning the first valid result, the first dirty result, or an error. So, the performance of a union check is strictly related to the possible options.

This changes the (sync) union mechanism to instead succeed fast. If the first option works, then it returns immediately. In the success case, this means that z.union can be faster than evaluating all the options. That's basically it. Also, it's a little more stingy with how it uses memory, by retaining less of results.

@scotttrinh

Copy link
Copy Markdown
Collaborator

This looks great! I'll take a better look tomorrow and try to think of possible unintended implications.

@colinhacks colinhacks merged commit 5b46d88 into colinhacks:master Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants