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)

  • Takes: input: string
  • Returns: same shape as normalizeColor

Cached version of normalizeColor for repeated string calls — see Detection & Parsing.

clearColorCache()

  • Takes: nothing
  • Returns: void

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

getCacheStats()

  • Takes: nothing
  • Returns: { 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()

  • Takes: nothing
  • Returns: void

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

disableCache()

  • Takes: nothing
  • Returns: 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.