Diagnosing unnecessary re-renders with subscribeWithSelector middleware
0 reputation · 20 Jun 2025, 22:14 UTC
Goal
Measure whether a component re-rendering on every store update is caused by an unstable selector reference, a missing shallow equality function, or genuinely changing state — before applying any optimization.
Constraints and uncertainty
The subscribeWithSelector middleware exposes store.subscribe(selector, listener, options) for slice-based subscriptions, yet the default Object.is comparison treats each inline object selector as a new reference. A component selecting {a: s.a, b: s.b} will re-render on any state change unless a custom equality function is supplied. The same store can be exercised in plain Node via store.subscribe without mounting React components, but the most reliable signal — Profiler render counts, selector logging, or listener invocation counts — depends on the environment and the shape of the selected slice.
Questions
- Which measurement approach (React DevTools Profiler, inline selector logging, or subscribe listener counting) gives the clearest signal for a given selector shape?
- How can a Node-only test verify that a selector with
shallowequality fires only when the selected slice actually changes? - At what object size does the cost of
shallowcomparison outweigh the re-renders it prevents?