Svelte 77 🧡 Signal Node Graph Execution, Push-Pull Evaluation, and Dependency Tracking
Svelte 5’s reactivity system operates on a directed acyclic graph (DAG) where every piece of reactive state, derived value, or effect is a node. When you declare state with $state or $derived, you are not just creating variables; you are constructing nodes in a global dependency graph. This architecture allows Svelte to treat reactivity as a graph-traversal problem rather than a string-parsing or top-level component-invalidation problem.
The execution model is a hybrid “push-pull” system. When a source node (like a $state) changes, it “pushes” a notification down the graph to mark downstream consumers as “dirty.” However, it does not re-execute the consumers immediately. Instead, it “pulls” the necessary updates only when a value is needed (e.g., during a DOM update or an effect trigger). This lazy evaluation strategy prevents unnecessary work and ensures that the UI remains consistent, even when multiple states change in the same event loop.
Key point: The Svelte 5 reactivity graph tracks nodes (signals) and dependencies (edges) dynamically, using a push-pull mechanism to mark nodes as dirty and pull fresh values only when required, guaranteeing “glitch-free” updates without redundant re-computations.
Why exists
The glitch avoidance problem. In simple event-based systems, a change can propagate along different branches of a tree at different speeds, leading to “intermediate” state values being rendered before all signals have settled. The graph system ensures consistency.
The overhead of component invalidation. In previous versions, Svelte often invalidated entire components when a single state changed. Graph execution allows Svelte to invalidate individual DOM nodes (or parts of DOM nodes), dramatically reducing the cost of updates.
The “untracked” logic challenge. Sometimes you need to read a state without creating a dependency. The node graph system provides a standard way to implement “untrack” scopes, which are essential for complex interactive logic.
The performance of lazy evaluation. By using a pull-based mechanism, Svelte avoids computing derived values if their consumers are currently hidden or inactive, shifting compute costs to the moment of visibility.
The shared state complexity. As applications grow, sharing state across distant components creates complex dependency chains. The graph structure provides a mathematical foundation that remains stable regardless of how deeply nested your components are.
a. The Graph Structure: Nodes and Edges
Every $state, $derived, and $effect is a node in the internal Svelte graph. When a node’s value is accessed within a reactive context (like another derivation or a component), Svelte establishes an “edge” from the state to the consumer. This tracking is done via a global “subscriber stack” that Svelte maintains during execution.
// Each of these is a node in the reactive DAG
let count = $state(0);
let double = $derived(count * 2);
// Edges:
// count -> double
// double -> UI text node
When count is read during double‘s evaluation, Svelte records that double depends on count. Because this is done at runtime, the dependency is dynamic. If your derivation logic contains a branch that is not taken (e.g., an if statement), Svelte won’t track the dependencies inside the un-taken branch.
b. The Push-Pull Evaluation Model
“Pushing” happens during mutation. When you write count++, the count node notifies double that it is now “dirty.” It does not force double to re-calculate immediately. “Pulling” happens when you ask for the value. When the UI or an effect asks for double, Svelte checks if it is dirty; if it is, Svelte triggers the re-computation of double (which in turn may “pull” from count).
// 1. Mutation (Push):
count.value++;
// The graph marks 'double' as dirty but does nothing else.
// 2. Access (Pull):
console.log(double);
// Svelte detects 'double' is dirty, pulls current 'count',
// re-calculates, and returns fresh value.
This model is critical for performance in batch updates. If you change count five times in one function, the graph will only compute the result of double once, at the end of the batch, when the UI finally asks for the value.
c. Dynamic Dependency Tracking
Dependency tracking is inherently dynamic because Svelte 5 does not scan your code for static dependencies. Instead, it performs “lazy observation.” Every time a reactive node (a function in $derived or $effect) executes, it clears its old dependencies and records whatever nodes it accesses during that specific execution.
let mode = $state('A');
let valueA = $state(1);
let valueB = $state(2);
let result = $derived(mode === 'A' ? valueA : valueB);
When mode is ‘A’, result depends only on mode and valueA. If valueB changes, result remains clean and does not re-compute. If mode changes to ‘B’, the dependency list is wiped and recalculated to point to mode and valueB. This allows for extremely complex, high-performance logic that adjusts to data patterns on the fly.
Complete Example Session
// ============================================
// PART 1: DEFINING THE GRAPH
// ============================================
let a = $state(1);
let b = $state(2);
let sum = $derived(a + b);
// ============================================
// PART 2: GRAPH TRAVERSAL (PUSH)
// ============================================
// Mutation triggers "dirty" flags
a = 10;
// Graph: sum is now "dirty"
// ============================================
// PART 3: GRAPH TRAVERSAL (PULL)
// ============================================
// Reading value triggers re-evaluation
console.log(sum);
// ============================================
// PART 4: DYNAMIC RE-LINKING
// ============================================
let condition = $state(true);
let dynamic = $derived(condition ? a : b);
// Svelte tracks [condition, a] initially
// If condition becomes false, it tracks [condition, b]
These parts show the push-pull mechanics and dynamic re-linking that make Svelte 5’s reactivity far more robust than static observer patterns.
Quick Reference
Node Graph Mechanics
| Concept | Action | Timing |
|---|---|---|
| Push | Invalidate/Dirty | During Mutation |
| Pull | Re-evaluate/Refresh | During Access |
| Edge | Record Dependency | During Execution |
Signal Node Types
| Node | Behavior |
|---|---|
$state | Source (Leaf node) |
$derived | Intermediate (Internal node) |
$effect | Sink (Leaf node) |
Best Practices
✅ Do This:
// Keep derived logic pure
let val = $derived(a + b); // ✅
// Rely on dynamic re-tracking
let val = $derived(condition ? a : b); // ✅
// Batch mutations to minimize graph traversals
count += 1; items.push(1); // ✅
❌ Don’t Do This:
// Don't perform side effects in $derived
let val = $derived(a + console.log(a)); // ❌
// Don't manually manage dependency lists
// (Svelte 5 has no need for manual deps) // ❌
// Don't rely on global state mutators in derivations
let val = $derived(a + globalVar); // ❌
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Unintended re-run | Logic in $derived changes deps | Keep derivations pure |
| Stale closure | Reading state inside non-reactive fn | Use runes in the scope |
| Performance hit | Huge number of signals | Use $state.raw for static data |
| Missing updates | Reading outside reactive scope | Wrap in $effect or template |
Real-World Examples
1. Dynamic Filter Graph
let filter = $derived(list.filter(i => i.val > threshold));
2. Conditional Graph Branching
let res = $derived(auth ? secureData : publicData);
3. Batching State Updates
// Svelte 5 batches updates before pulling
user.name = 'x'; user.id = 'y';
4. Avoiding Tracking
import { untrack } from 'svelte';
let val = untrack(() => expensiveRead());
5. Graph cleanup on destruction
$effect(() => { /* setup */ return () => { /* teardown */ }});
6. Multi-level Derivation
let d1 = $derived(a + b);
let d2 = $derived(d1 * 2);
7. Array item tracking
// List is a graph source, index access is an edge
let item = $derived(list[index]);
8. Object property tracking
// Only changes to user.name trigger updates
let name = $derived(user.name);
9. Effect synchronization
$effect(() => {
if (count > 10) console.log('Threshold');
});
10. Memory efficient derived
// Logic re-runs only if deps actually change
let total = $derived.by(() => { ... });
Visual
The Reactive DAG (Directed Acyclic Graph)
┌─────────────────────────────────────────────────────────┐
│ (State A) ───┐ │
│ │ (Dependency) │
│ ▼ │
│ (Derived B) ───► (Effect/UI C) │
│ ▲ │
│ │ (Dependency) │
│ (State D) ───┘ │
└─────────────────────────────────────────────────────────┘
Push-Pull Execution Timing
┌─────────────────────────────────────────────────────────┐
│ State Mutation ──► Mark Dirty ──► (Wait...) │
│ │
│ Access Request ──► Pull (Refresh) ──► DOM Update │
└─────────────────────────────────────────────────────────┘
Dynamic Re-linking
┌─────────────────────────────────────────────────────────┐
│ Initial: [Condition A] ──► [Derived B] │
│ │
│ After Toggle: │
│ Condition A is unlinked, Condition C is linked │
│ [Condition C] ──► [Derived B] │
└─────────────────────────────────────────────────────────┘
Summary
| Item | Value |
|---|---|
| Graph Model | Directed Acyclic Graph (DAG) |
| Evaluation | Hybrid Push-Pull |
| Dependency Tracking | Dynamic / Runtime |
| Glitch Avoidance | Native to DAG structure |
| Efficiency | Lazy / On-demand calculation |
Key takeaways:
- Everything is a node.
$state,$derived, and$effectconstruct a unified graph of reactive dependencies that spans across files and components. - Push-pull keeps it efficient. Mutation only marks the graph as “dirty,” while actual work is delayed until the value is pulled by a consumer, avoiding wasted compute.
- Tracking is dynamic. Dependencies are not fixed at compile-time; they are recalculated every time a node executes, allowing for branching and dynamic dependency paths.
- Consistency is guaranteed. By treating the reactivity as a graph, Svelte eliminates inconsistent states (glitches) where components might see partial updates.
- Keep derivations pure. Because of the reactive tracking system, side effects inside
$derivedcan lead to unpredictable graph behavior; use$effectfor those instead.
Remember: The Svelte 5 graph architecture is a masterclass in reactive engineering. By moving away from static observers to a dynamic, lazy-pulling graph, Svelte ensures that your UI is not only fast but logically sound. The system handles the “plumbing” of your application, leaving you to focus on the business logic of how your data transforms.
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!