Skip to content

Cache ​

An in-memory memoization cache for normalizeColor — useful in rendering loops, color pickers, or any context with repeated calls on the same string values. It's a plain Map keyed by the input string, not an LRU cache — it has no size limit or eviction policy, and only ever shrinks when you clear it yourself.

normalizeColorCached(input) ​

Same shape as normalizeColor's return value.

Cached version of normalizeColor for repeated string calls.

clearColorCache() ​

void

Clears every cached entry and resets the hit counter to 0.

getCacheStats() ​

{ size: number, hits: number }

size is the number of distinct strings currently cached; hits is the running count of cache hits since the last clearColorCache().

enableCache() ​

void

Re-enables the cache. On by default — only needed after a prior disableCache().

disableCache() ​

void

Disables caching — normalizeColorCached falls through to plain normalizeColor calls. Useful in tests where you don't want state leaking between cases.

ts
import { normalizeColorCached, getCacheStats, clearColorCache } from 'color-value-tools'

// First call — parsed and cached
normalizeColorCached('#3498db')

// Second call — instant cache hit
normalizeColorCached('#3498db')

getCacheStats() // { size: 1, hits: 1 }
clearColorCache() // cache and hit counter reset to empty

Since the cache never evicts entries on its own, call clearColorCache() (or disableCache()) periodically in long-running processes that parse many distinct, rarely-repeated color strings — otherwise the Map grows for the lifetime of the process.