const MAX_FORMATTER_CACHE_SIZE = 128; export function getCachedFormatter( cache: Map, locale: string, options: Record, create: (locale: string, options: Record) => TFormatter ): TFormatter { const key = `${locale}|${stableOptionsKey(options)}`; const cached = cache.get(key); if (cached) return cached; const formatter = create(locale, options); if (cache.size >= MAX_FORMATTER_CACHE_SIZE) { const oldestKey = cache.keys().next().value; if (oldestKey) cache.delete(oldestKey); } cache.set(key, formatter); return formatter; } function stableOptionsKey(options: Record): string { return JSON.stringify( Object.entries(options) .filter(([, value]) => value !== undefined) .sort(([left], [right]) => left.localeCompare(right)) ); }