A structural diff algorithm recursively compares two nested maps and records how the second differs from the first without depending on map-field order. It supports replacement and addition, while deletion remains deliberately unsupported. For arrays, it compares elements by position and returns the changed second value. section 5.4 section 5.6
The algorithm reduces reconciliation to a small set of structural cases:
- Recurse through corresponding map fields, taking the union of keys from both maps. section 5.4
- Return only changed branches: replacements contain the second value, additions contain newly present fields, and equal branches disappear from the result. section 5.4 section 5.6
- Compare arrays in order; unchanged arrays produce no diff, while changed positions carry values from the second array. section 5.4
Finding concurrent state changes
Suppose a library state changes a book's publication year, then changes its title and an author's name. Comparing the original state with the first update yields only the nested publication-year path; comparing it with the later state yields the title and author-name paths. Those paths can reveal whether two mutations overlap before applying a patch. section 5.4 section 5.6
Table 5.3 Kinds of structural differences between maps without nested fields (view table figure)
Use the difference table to compare replacement, addition, and unsupported deletion for flat maps before following the recursive nested-map implementation. section 5.4
