Repository navigation
Look up a cached color without reordering the cache - #60502
Merged
Merged
Conversation
Why === `normalizeColor` caches parsed color strings in a `Map` of up to 1,024 entries. Every hit deletes and re-inserts its entry to keep LRU order. That makes a hit cost three times what a single `get` would, and more than parsing a named color like `red`. How === A hit is now a single `get`, and eviction is FIFO. Cached values are never `undefined`, so `get` alone tells a hit from a miss. A color in use is evicted after 1,024 newer strings and is then parsed again. Instructions per `normalizeColor` call on Hermes, including about 75 for the benchmark loop: | Color | Parsed | Cached, before | Cached, after | | --- | ---: | ---: | ---: | | `'#e5e5ea'` | 5,100 | 1,831 | 567 | | `'red'` | 1,477 | 1,838 | 566 | | `'rgba(0, 122, 255, 0.5)'` | 39,632 | 1,845 | 574 | | `'hsl(120, 50%, 50%)'` | 23,926 | 1,833 | 567 | | `'not a color'` | 12,199 | 1,830 | 575 | Dropping LRU looks counterintuitive, but it is faster for most apps. LRU order only matters once the cache is full, and most apps use far fewer than 1,024 color strings. As a real-world example, Bluesky's web app, browsed logged out in light and dark themes, used 64 distinct color strings across 49,021 calls. Test Plan ========= `yarn jest packages/normalize-color` passes, including a new test that a cached `0` or `null` is returned without parsing again. The instruction counts are from 200,000 calls under Hermes, minus a run with no calls, best of three.
ide
marked this pull request as ready for review
December 3, 2026 05:38
javache
approved these changes
Dec 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary:
normalizeColorcaches parsed color strings in aMapof up to 1,024 entries. Every hit deletes and re-inserts its entry to keep LRU order. That makes a hit cost three times what a singlegetwould, and more than parsing a named color likered.A hit is now a single
get, and eviction is FIFO. Cached values are neverundefined, sogetalone tells a hit from a miss. A color in use is evicted after 1,024 newer strings and is then parsed again.Instructions per
normalizeColorcall on Hermes, including about 75 for the benchmark loop:'#e5e5ea''red''rgba(0, 122, 255, 0.5)''hsl(120, 50%, 50%)''not a color'Dropping LRU looks counterintuitive, but it is faster for most apps. LRU order only matters once the cache is full, and most apps use far fewer than 1,024 color strings.
As a real-world example, Bluesky's web app, browsed logged out in light and dark themes, used 64 distinct color strings across 49,021 calls.
Changelog:
[General] [Changed] - Look up cached colors in
@react-native/normalize-colorswithout reordering the cacheTest Plan:
yarn jest packages/normalize-colorpasses, including a new test that a cached0ornullis returned without parsing again. The instruction counts are from 200,000 calls under Hermes, minus a run with no calls, best of three.