liveBook · Manning

Reconciliation

As explained in Build a Frontend Web Framework (From Scratch)

The process of finding differences between two virtual DOM trees.

When application state changes, the view produces a new virtual DOM tree. Reconciliation compares it with the tree used for the current view, then updates the browser’s DOM. Rebuilding the whole DOM would work, but it can replace nodes that did not need to change. The goal is a reasonably small set of operations, not a promise of the absolute minimum. section 7.0 section 7.2

There are two jobs: diffing finds changes between the trees; patching applies them to the real DOM. On the first render, mount the view. On later state changes, call patchDOM(oldVdom, newVdom, parentEl) and save the resulting virtual DOM for the next comparison. section 7.7 section 7.3

Build the comparison from three smaller helpers:

  • objectsDiff(oldObj, newObj) reports added, removed, and updated keys. Use it for attributes and CSS styles. section 7.4 section 8.0
  • arraysDiff() finds additions and removals in arrays of CSS classes. section 7.1 section 8.0
  • arraysDiffSequence() plans operations that turn one child array into another. Work through the new array one position at a time, recording removals, additions, moves, and no-ops while tracking original indices. section 7.6.2 section 7.6.3

Start at the roots and continue recursively through matching nodes. If two nodes cannot be reused, replace the old DOM node and its subtree instead of comparing its descendants. Otherwise, update text through nodeValue or patch an element’s attributes, classes, styles, and event listeners. Apply the planned child operations, then continue into children that remain or have moved. Text and fragment nodes can be reused; element nodes need the same tag name. section 8.2.1 section 8.5

Figure 8.4 The reconciliation algorithm flow chart
figure

Follow the flow chart from the node comparison to either subtree replacement or property and child patching. section 8.2.1

Check that unchanged nodes stay put

In a TODOs view, type into an input while watching the DOM in browser developer tools. A rebuild highlights the whole tree. With patching, typing initially changes only the input’s value property; after three characters, removing the Add button’s disabled attribute highlights that button. Keeping the input rather than replacing it also preserves focus while typing. section 8.4.1 section 8.0

This version has a limit worth checking: elements with the same tag compare as equal even if they represent different list items. When such items swap, reconciliation may recreate unchanged descendants rather than recognize the move. Comparing keys as well as tags addresses that case. section 12.2

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Works through object and array diff helpers, then defines recursive node comparison and DOM patching steps.
Examples
Uses shopping-list changes, a worked array-operation sequence, and DOM inspection of a TODOs input.
What it assumes
Assumes JavaScript objects, arrays, and DOM nodes while defining reconciliation and developing its helpers step by step.
What you can do after
Build it from scratch with diff helpers and patchDOM(), then diagnose unnecessary DOM changes in browser developer tools.
How it connects
Connects virtual DOM, application state, DOM patching, CSS classes, event listeners, and keyed lists.

Also explained in

Compare how 7 books explain reconciliation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.