State & ArchitectureExperimental

Signals and Fine-Grained Reactivity (Reactive Graph)

Mental Model

A Signal wraps an observable value that automatically tracks reads inside a computed or effect. Writes propagate updates only to the exact nodes reading that value, in O(1) relative to DOM size.

Interactive Simulator

State Propagation: VDOM Diff vs Signals

Lines of Code
Before
4
After
3
Reflows / Browser Cost
Before
O(N) component diff
After
O(1) subscribed nodes
BeforeVirtual DOM Subtree Re-render
function Parent() {
  const [count, setCount] = useState(0);
  return <ExpensiveTree count={count} />;
}
Lines: 4Reflows: O(N) component diffDependencies: Manual React memoization

Requires memo/useCallback to prevent cascading re-renders.

After (Native)Fine-Grained Signal (Targeted O(1))
const count = new Signal.State(0);
const doubled = new Signal.Computed(() => count.get() * 2);
// Only the subscribed text node updates
Lines: 3Reflows: O(1) subscribed nodesDependencies: Native reactive graph

Zero virtual tree diffing on each mutation.

Live Reactive Node:doubled = 0

Under the Hood

  1. Automatic Tracking: When a computed or effect runs, it pushes its context onto a global tracking stack; each signal.get() adds a signal -> observer edge.
  2. Push-Pull Propagation: Calling signal.set(v) marks descendants Dirty (Push) without eagerly recomputing lazy derived values, avoiding wasted V8 Hidden Classes allocations on unused branches.
  3. Lazy Evaluation (Pull): A computed only recomputes when read, and short-circuits downstream effects (often flushed on the Event Loop microtask checkpoint) if its new value equals the previous one.
JS
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 useEffect dependency array that everyone eventually misconfigures.
  • 2024+ (TC39 Signals Proposal): Standardizing a glitch-free reactive primitive shared across frameworks.

Production Pitfalls

Production Pitfall
  • Effects writing to tracked signals (infinite loop): An effect mutating 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.
When Not to Use
  • When not to use Signals: For simple forms or mostly static pages, native DOM primitives avoid unnecessary abstraction overhead; for multi-step workflows with illegal state combinations, pair or replace raw signals with Finite State Machines.

Connections & Logical Next Steps

State & ArchitectureSignals and Fine-Grained Reactivity (Reactive Graph)

Mechanical Comparisons