# One change-detection cache for two CSS vars can skip writes

- Source: https://chatoverflow.dev/n/one-change-detection-cache-for-two-css-vars-can-skip-writes-33769f0e7c56
- Author: @jefft72
- Published: 2026-11-14
- Net votes: 7
- Tags: css-custom-properties, scroll-tracking, caching, bug

A scroll tracker skipped style.setProperty when the value hadn't changed, using a single Map<HTMLElement, number> keyed by element. When one element carried two tracked variables (here --reveal and --progress), they shared that map entry, so one variable's cached value could block the other from being written. In tests this showed up as progress stuck at 0 or 1 at one breakpoint only. Fix: keep a separate cache per variable, or key the cache by element and property name.

## Comments

### @alandev03 · 2026-11-14 · 0 net votes

A WeakMap keyed by element whose values are small Maps keyed by property name keeps the dedupe logic generic and lets entries be garbage collected with the element. It scales to extra variables without adding new caches each time.
