Signals and Fine-Grained Reactivity (Reactive Graph)
Reactive model built on a directed acyclic dependency graph (Signal, Computed, Effect) updating only subscribed DOM nodes without virtual tree diffing.
About this page
State Propagation: VDOM Diff vs Signals
function Parent() {
const [count, setCount] = useState(0);
return <ExpensiveTree count={count} />;
}Requires memo/useCallback to prevent cascading re-renders.
const count = new Signal.State(0);
const doubled = new Signal.Computed(() => count.get() * 2);
// Only the subscribed text node updatesZero virtual tree diffing on each mutation.
doubled = 0Under the Hood#
- Automatic Tracking: When a
computedoreffectruns, it pushes its context onto a global tracking stack; eachsignal.get()adds asignal -> observeredge. - Push-Pull Propagation: Calling
signal.set(v)marks descendantsDirty(Push) without eagerly recomputing lazy derived values, avoiding wasted V8 Hidden Classes allocations on unused branches. - Lazy Evaluation (Pull): A
computedonly recomputes when read, and short-circuits downstream effects (often flushed on the Event Loop microtask checkpoint) if its new value equals the previous one.
const count = signal(1);
const doubled = computed(() => count.get() * 2);
effect(() => {
// Re-runs ONLY when `doubled` actually changes
badgeEl.textContent = String(doubled.get());
});Problem Genealogy#
- 2010 (KnockoutJS Observables): Early reactive graph with auto-tracking, but without glitch-free guarantees.
- 2013 (React Virtual DOM): Full subtree re-rendering plus diff reconciliation to simplify the mental model, trading CPU cycles for a
useEffectdependency array that everyone eventually misconfigures. - 2024+ (TC39 Signals Proposal): Standardizing a glitch-free reactive primitive shared across frameworks.
Production Pitfalls#
- Effects writing to tracked signals (infinite loop): An
effectmutating a signal it reads re-triggers the graph indefinitely until stack overflow. - Losing reactivity via destructuring: Extracting
const v = signal.get()outside the tracked execution scope breaks dynamic subscription.
Where this connects
Prerequisites
Event Loop, Macrotasks, and Microtask Queue
Reactive effect batching commonly relies on a microtask flush.
Next & related
Runtime Proxy Reactivity vs Static Compiler Graphs
Both proxy stores and compiled reactivity build fine-grained dependency edges between state and DOM.
Finite State Machines for UI
Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.
Mechanical Comparisons
- Contrasts WithSignals and Fine-Grained Reactivity (Reactive Graph) vs Finite State Machines for UI
Signals propagate arbitrary reactive value changes, whereas finite state machines enforce explicit transition guards.
- Contrasts WithSignals and Fine-Grained Reactivity (Reactive Graph) vs Stale Closures in Hooks & Callbacks
Signals pass a stable box reference whose .get() always reads the current value without stale closures.
- Contrasts WithSignals and Fine-Grained Reactivity (Reactive Graph) vs Virtual DOM & Reconciliation
VDOM diffs whole component subtrees on state changes; Signals update subscribed DOM nodes in O(1).