The DriftJS Reactivity Engine
Most reactive frameworks introduce significant runtime machinery to detect and propagate state changes. React uses fiber tree reconciliation; Solid and Vue use Proxy traps and signal graph nodes.
DriftJS takes a radically different approach: Compile-Time Variable Tracking + Bytecode Program Counter (PC) Invalidation.
How DriftJS Tracks State
When the compiler processes your .drift SFC, it executes three static passes:
- Declared Variable Detection (
declaredVars): The compiler inspects AST statements in the<script>block and identifies all mutable identifiers (e.g.let count = 0; let items = [];). - Expression Dependency Extraction:
Every interpolation
{ count + 1 }, directive condition@if count > 0, and attribute bindingclass={theme}is parsed with Acorn. The compiler inspects the AST identifiers and records which declared variables each expression depends on. - Reactive Binding Table (
ReactiveBinding[]): The compiler constructs a binding table mapping variable names to their exact bytecode Program Counter (PC) positions:// Example ReactiveBinding entry { name: "count", type: "variable", positions: [8, 24] // Bytecode PCs that reference `count` }
State Mutation & Dirty Notification
In your component, state is updated via plain JavaScript assignment:
<script>
let count = 0;
function inc() {
count++; // or count = count + 1;
}
</script>
Try it live — every increment marks count dirty, batches a microtask flush, and patches only the text node:
✦ Edit the code above — changes execute instantly in your browser.
At runtime, the component scope is configured with a change listener:
User Action (click) ──► Mutate Variable ──► markDirty("count") ──► queueMicrotask(flush)
- Mutation: Modifying
countcalls the internalmarkDirty("count")hook. - Microtask Batching: The VM pushes
"count"to adirtyVarsSet and checks if a flush is already scheduled. If not, it calls:
Multiple rapid mutations within the same event tick (e.g., updating 10 variables in a loop) collapse into a single microtask flush.queueMicrotask(() => this.flushDirtyUpdates());
The Update Phase: Direct In-Place Patching
When flushDirtyUpdates() runs, the VM does not re-render the component or compare a new DOM tree.
Instead:
- The VM loops over the variables in
dirtyVars. - For each variable, it looks up its
positionsinreactiveBindings. - It jumps directly to those bytecode PC positions and re-executes only the target instruction:
INTERPOLATE_TEXTat PC 8: The VM evaluates the compiled expression from the constant pool against the current scope, compares the result to the previous value, and if changed, sets:textNode.nodeValue = String(newValue);SET_ATTRat PC 14: The VM patches the attribute or DOM property on the element held in registerr0directly.REACTIVE_IFat PC 22: The VM re-evaluates the condition and toggles or updates the subtree between comment anchors.
Why This Delivers Maximum Performance
1. Zero Garbage Collection Churn
Because no virtual DOM nodes or temporary objects are created during updates, heap memory remains virtually flat.
2. Predictable O(1) Updates
Updating an interpolated number in a table of 10,000 rows executes in fractions of a microsecond. The VM performs a direct array lookup in registers and updates the text node without traversing parents or siblings.
3. Transparent Mental Model
You write standard JavaScript. No special .value accessors, no useCallback, no dependency arrays, and no risk of stale closures.