How does the LWC reactivity engine manage property observation and virtual DOM updates?
Answer
Basics: Since Spring '20, all class fields are reactive for primitive values: if a field is used in the template (or in a getter the template uses) and its value changes, the component re-renders. For objects and arrays, reactivity applies to reassignment (this.list = [...this.list, item]), not to in-place mutation, unless the field is decorated with @track.
How it works: During render() the engine records which reactive fields and @api properties are accessed. A set operation on one of them (or a mutation seen through the @track proxy) marks the component dirty. Dirty components are queued and re-rendered once in a microtask, so several changes in one tick cause a single render. The template produces a virtual DOM tree, which is diffed against the previous one (keyed for for:each), and only changes are written to the real DOM. Then renderedCallback fires.
Advanced / deep dive: @track uses an observable membrane (Proxy-based) that wraps plain objects and arrays deeply, so mutations such as this.obj.a.b = 1 or this.arr.push(x) are detected. Class instances, Date, Map and Set are not deeply tracked. Values passed from a parent via @api are read-only proxies, so a child cannot mutate them; it must copy or dispatch an event. Fields not read during render do not cause re-renders when changed, which is an optimization (use plain non-template fields for internal state). Getters have no memoization: they run every render. Wire results are frozen. Infinite loops come from changing reactive state in renderedCallback unconditionally. For performance, avoid creating new arrays and objects each render in getters, and use stable keys so the diff can reuse nodes.