Skip to content

Commit 51f7828

Browse files
committed
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.
1 parent d1204ac commit 51f7828

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎packages/normalize-color/__tests__/normalizeColor-test.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,18 @@ it('returns the same color when it is already normalized', () => {
177177
const normalizedColor = normalizeColor('red') || 0;
178178
expect(normalizeColor(normalizedColor)).toBe(normalizedColor);
179179
});
180+
181+
it('returns a cached 0 or null without parsing again', () => {
182+
for (const [color, expected] of [
183+
['#0000', 0x00000000],
184+
['not a color', null],
185+
]) {
186+
expect(normalizeColor(color)).toBe(expected);
187+
const exec = jest.spyOn(RegExp.prototype, 'exec');
188+
const cached = normalizeColor(color);
189+
const execCalls = exec.mock.calls.length;
190+
exec.mockRestore();
191+
expect(cached).toBe(expected);
192+
expect(execCalls).toBe(0);
193+
}
194+
});

‎packages/normalize-color/index.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ function normalizeColor(color) {
2626
return null;
2727
}
2828

29-
if (cachedColors.has(color)) {
30-
// Map iteration order follows insertion order, so re-inserting on every
31-
// hit keeps the least-recently-used entry first.
32-
const cachedColor = cachedColors.get(color);
33-
cachedColors.delete(color);
34-
cachedColors.set(color, cachedColor);
29+
// A single get() is faster than LRU and most apps don't fill the color cache
30+
const cachedColor = cachedColors.get(color);
31+
if (cachedColor !== undefined) {
3532
return cachedColor;
3633
}
3734
const normalizedColor = parseColorString(color);

0 commit comments

Comments
 (0)