Skip to content

Look up a cached color without reordering the cache - #60502

Merged
javache merged 1 commit into
react:mainfrom
expo:@ide/color-cache-single-get
Dec 3, 2026
Merged

javache merged 1 commit into
react:mainfrom
expo:@ide/color-cache-single-get

Conversation

@ide

@ide ide commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary:

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.

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.

Changelog:

[General] [Changed] - Look up cached colors in @react-native/normalize-colors without reordering the cache

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.

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
ide marked this pull request as ready for review December 3, 2026 05:38
@javache
javache merged commit f559f3c into react:main Dec 3, 2026
92 of 93 checks passed
@ide
ide deleted the @ide/color-cache-single-get branch December 4, 2026 12:57
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.

2 participants