From 51f7828ef19e4b8f0d42003ef3dd1ec521411e8d Mon Sep 17 00:00:00 2001 From: James Ide Date: Wed, 2 Dec 2026 21:36:52 -0800 Subject: [PATCH] Look up a cached color without reordering the cache 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. --- .../__tests__/normalizeColor-test.js | 15 +++++++++++++++ packages/normalize-color/index.js | 9 +++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/normalize-color/__tests__/normalizeColor-test.js b/packages/normalize-color/__tests__/normalizeColor-test.js index ab84c6d3d327..166b86e796eb 100644 --- a/packages/normalize-color/__tests__/normalizeColor-test.js +++ b/packages/normalize-color/__tests__/normalizeColor-test.js @@ -177,3 +177,18 @@ it('returns the same color when it is already normalized', () => { const normalizedColor = normalizeColor('red') || 0; expect(normalizeColor(normalizedColor)).toBe(normalizedColor); }); + +it('returns a cached 0 or null without parsing again', () => { + for (const [color, expected] of [ + ['#0000', 0x00000000], + ['not a color', null], + ]) { + expect(normalizeColor(color)).toBe(expected); + const exec = jest.spyOn(RegExp.prototype, 'exec'); + const cached = normalizeColor(color); + const execCalls = exec.mock.calls.length; + exec.mockRestore(); + expect(cached).toBe(expected); + expect(execCalls).toBe(0); + } +}); diff --git a/packages/normalize-color/index.js b/packages/normalize-color/index.js index d4ea4682eebe..246d8288e8b8 100644 --- a/packages/normalize-color/index.js +++ b/packages/normalize-color/index.js @@ -26,12 +26,9 @@ function normalizeColor(color) { return null; } - if (cachedColors.has(color)) { - // Map iteration order follows insertion order, so re-inserting on every - // hit keeps the least-recently-used entry first. - const cachedColor = cachedColors.get(color); - cachedColors.delete(color); - cachedColors.set(color, cachedColor); + // A single get() is faster than LRU and most apps don't fill the color cache + const cachedColor = cachedColors.get(color); + if (cachedColor !== undefined) { return cachedColor; } const normalizedColor = parseColorString(color);