(line: string)
| 8 | const MAX_CACHE_SIZE = 4096 |
| 9 | |
| 10 | export function lineWidth(line: string): number { |
| 11 | const cached = cache.get(line) |
| 12 | if (cached !== undefined) return cached |
| 13 | |
| 14 | const width = stringWidth(line) |
| 15 | |
| 16 | // Evict oldest 25% when cache grows too large. Full clear would force |
| 17 | // re-measuring every visible line on the next frame. |
| 18 | if (cache.size >= MAX_CACHE_SIZE) { |
| 19 | const evictCount = Math.floor(cache.size * 0.25) |
| 20 | let evicted = 0 |
| 21 | for (const key of cache.keys()) { |
| 22 | cache.delete(key) |
| 23 | if (++evicted >= evictCount) break |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | cache.set(line, width) |
| 28 | return width |
| 29 | } |
no test coverage detected