replace isTokenMatch with wcmatch for 5x speedup #497
Merged
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.
Changes
Hi Drew! This PR removes all uses of
isTokenMatch, instead callingwcmatchoutside of loops and then using the returned function inside loops. I found that this dropped Figma's token build times from 35s down to 7s.I left
isTokenMatchsince it's exported from@terrazzo/token-tools, but marked it as@deprecated. Not sure if that's the right move here, but given that it can lead to a big perf hit if used too much, I think it could be removed in the future.Let me know if you think the
wcmatch(patterns)call should be wrapped in a helper intoken-tools, I could see it handling null inputs and returning a dummy function so callers don't have to handle that themselves.I was profiling
tz buildand thetransformfunction from wildcard-match was dominating the profile. I think it was one of those hidden quadratic complexity things because callingwcmatch(patterns)does a bunch of work to build up an efficient regex, but that work was being done repeatedly on the same patterns when it could be done once.CPU-20250621T224815.cpuprofile
How to Review
This is mostly a find and replace of the
isTokenMatchfunction, so it should be easy to review. Thewcmatchfunction is called in the highest scope possible where the matching patterns can be compiled once by wcmatch, and then reused within loops.