Fine-Grained Signals & Glitch-Free Topological Evaluation
Eliminates double-renders and torn intermediate state in diamond dependency graphs. Push-pull topological evaluation marks downstream nodes dirty in one pass and lazily recomputes only what effects actually read.
About this page
Diamond Graph Push-Pull Evaluation
Trace how a single write to Signal A propagates Dirty and Check marks before Effect D lazily evaluates once with consistent values.
All four nodes are synchronized. Source A holds 1; derived nodes B (A * 2) and C (A + 1) hold 2; sink D (B + C) holds 4 with 1 total execution.
const A = signal(1);
const B = computed(() => A() * 2);
const C = computed(() => A() + 1);
const D = effect(() => B() + C());- A
- 1
- B
- 2 (Clean)
- C
- 2 (Clean)
- D
- 4 (Clean)
- dExecutions
- 1
- glitchDetected
- false
Under the Hood#
A reactive runtime maintains a directed acyclic graph (DAG) on the JavaScript main thread heap. Rather than allocating Set instances per node, modern engines store dependencies as doubly-linked list nodes connecting sources to subscribers in O(1) time. Every derived node carries a tri-state flag (Clean, Check, or Dirty) and a monotonic version counter.
This scheduling layer powers fine-grained signals and replaces top-down Virtual DOM reconciliation with targeted graph invalidation. When a source signal updates, the engine executes four deterministic steps:
- Push Phase (Eager Marking): Writing to source
Aincrements its version and traverses outgoing subscriber edges. Direct dependents (B,C) are markedDirty; transitive dependents (D) are markedCheck. No derived user code executes during this pass. - Effect Scheduling: Any reactive sink (DOM binding or
effect) reached during the push phase is appended to a pending flush queue without running immediately. - Pull Phase (Topological Verification): When an effect flushes or user code reads
D, the runtime inspectsD's state. BecauseDisCheck, it recursively callsupdateIfNecessary()on upstream sources (B, thenC) before runningD's own computation. - Equality Cutoff & Edge Pruning: When
Brecomputes, the engine compares the new output against the cached value viaObject.is. If neitherBnorCchanged,Dtransitions back toCleanwithout executing. Dynamic edges recorded via Proxy or compiler reactivity that were not touched during the run are unlinked.
const enum State { Clean, Check, Dirty }
function updateIfNecessary(node: ComputedNode): void {
if (node.state === State.Clean) return;
if (node.state === State.Check) {
for (const src of node.sources) {
updateIfNecessary(src);
if (node.state === State.Dirty) break;
}
}
const next = node.state === State.Dirty ? node.compute() : node.value;
if (!Object.is(node.value, next)) { node.value = next; markChildrenDirty(node); }
node.state = State.Clean;
}All traversal runs synchronously on the main thread Call Stack. Memory cost scales linearly with active edge count (O(V + E)), while CPU cost skips untouched subgraphs entirely when an upstream equality check short-circuits.
Problem Genealogy#
- 2010: Naive Push Observables (Knockout.js, early RxJS): Synchronous depth-first push notifications fired
Dimmediately afterBupdated, beforeCsaw the newA. A single upstream write triggered 2x redundant DOM updates and exposed impossible transient states (Bnew,Cstale). - 2015: Topological Push-Pull (MobX): Introduced two-phase propagation with observer state flags, eliminating diamond glitches at the cost of
Setallocation overhead and extra object wrappers per observable property. - 2018-2022: Tri-State & Linked-List Graphs (SolidJS, Reactively, Preact Signals): Standardized
Clean/Check/Dirtycoloring with doubly-linked dependency lists, cutting per-signal heap footprint and eliminating GC churn during dynamic branch switching. - 2024-2026: TC39 Signals Proposal: Consolidated push-pull glitch-free evaluation into a Stage 1 ECMAScript specification so frameworks can share one underlying graph scheduler.
Production Pitfalls#
- Stack overflow on cyclic derivations: Reading a computed signal inside its own evaluation chain (
A -> B -> A) throwsRangeError: Maximum call stack size exceededduring the recursive pull phase. Track a transientComputingflag on each active node and throw an explicit cycle error before recursing. - Silent heap growth in dynamic branches: Conditional reads (
flag() ? a() : b()) retain dead references toaafterflagflips false if the graph only appends subscriptions without pruning. Stamp every edge with an evaluation epoch and unlink stale incoming sources when a node finishes recomputing. - Unbatched multi-write flushes causing layout thrashing: Mutating five leaf signals sequentially in a loop flushes downstream DOM effects five separate times, triggering repeated style recalculations. Wrap synchronous multi-signal writes inside a
batch()transaction so the push phase completes before effects flush once.
Where this connects
Prerequisites
Runtime Proxy Reactivity vs Static Compiler Graphs
Depends on runtime Proxy traps or compile-time getter wrappers to record dynamic dependency edges during pull evaluation.
Signals and Fine-Grained Reactivity (Reactive Graph)
Defines the directed acyclic graph traversal and tri-state dirty-checking machine that keeps fine-grained signal graphs glitch-free.
Next & related
Virtual DOM & Reconciliation
Avoids top-down component tree diffing by propagating exact invalidation marks directly along subscribed graph edges.
Mechanical Comparisons
- Contrasts WithFine-Grained Signals & Glitch-Free Topological Evaluation vs Virtual DOM & Reconciliation
Avoids top-down component tree diffing by propagating exact invalidation marks directly along subscribed graph edges.