Context
Clearing a TreeStore takes time quadratic in its visible node count. Store#afterSetData() clears before it adds, so every data reload of a large flat or expanded tree pays the same cost.
Measured on dev 5dcacbe with a unit probe (Turbo Mode, Node, ms):
| Case |
clear() / reload |
TreeStore, 10,000 flat visible nodes |
75 |
TreeStore, 20,000 flat visible nodes |
639 |
TreeStore, 40,000 flat visible nodes |
1,769 |
Store, 40,000 rows (control) |
0 |
TreeStore, 50 × 800 collapsed, 50 visible (control) |
1 |
TreeStore, 50 × 800 expanded, 40,050 visible |
777 |
TreeStore, 40,000 flat, store.data = rows |
1,532 |
The Problem
TreeStore#splice() runs two O(N) scans once per removed node:
- Step 1b decides visibility with
me.indexOf(node) > -1. Collection#indexOf() is _items.indexOf(...), a scan of the projection.
- The removal reaches
Collection#splice() as an array of items, whose per-item path runs items.splice(me.indexOfKey(key), 1): a scan plus a shift for each item. clear() arrives as the numeric form splice(0, count), and the override converts it to that array form (_items.slice(...)), so the Collection's one-pass numeric path is never taken.
Each scan is quadratic on its own. At 40,000 flat visible nodes, fixing only the first leaves clear() at 1,580 ms. Fixing only the second leaves 501 ms, still quadratic (52, 197, 501 ms at 10k, 20k, 40k). Fixing both gives 31 ms, and the reload drops from 1,532 to 85 ms.
The Architectural Reality
src/data/TreeStore.mjs clear() wipes #allRecordsMap and #childrenMap, then calls super.clear(). That reaches Collection#clear(), whose this.splice(0, this.count) dispatches back into the splice() override.
splice() step 1b: if (me.indexOf(node) > -1) visibleToRemove.push(node). Step 4: super.splice(insertIndex, visibleToRemove, visibleToAdd, silent).
- The projection's key map,
me.map, holds exactly the visible keys (#rebuildKeysAndCount() rebuilds it from _items), and Collection#splice() removes by key. map.has(key) therefore answers the same question in O(1).
- Design authority: the
TreeStore#addChild JSDoc, "Ingestion is already O(N) over a level, so a per-node key scan makes a bulk load O(N²) … in the one class whose whole premise is 50k-record ingestion."
The Fix
In TreeStore#splice():
- Step 1b:
me.map.has(key) instead of me.indexOf(node) > -1.
- Step 4: when the call removes the whole projection (
index === 0 && removeCountOrToRemoveArray === me.count), delegate the numeric form, super.splice(0, me.count, visibleToAdd, silent), so the Collection drops the projection in one pass.
Acceptance Criteria
Out of Scope
- Bulk removal of a non-contiguous subset (
store.remove(manyNodes)). The Collection's array removal stays per-item, here and for plain Stores; it gets its own ticket if measured to matter.
- Step 2a's
me.indexOf(parentNode) check when adding under an expanded parent, which is not on the clear path.
Related
TreeStore#addChild: the same class's bulk-load O(N²) fix, and the precedent for measuring at 20k+ nodes.
- #9432: the
clear() override against the split-brain leak. Its requirement that clear() calls super.clear() holds under this fix.
- #9404: the Tree Grid epic. It is past the sub-issue cap, so this stays standalone.
Live latest-open + closed sweep: latest 20 open, plus open and closed searches for TreeStore, TreeStore clear and TreeStore performance quadratic, at 2026-11-03T05:02Z. The same-surface closed hit is #9432 (Out of Scope / Avoided Traps read); no equivalent. A2A claim sweep: last 30 messages, all read states, no claim on TreeStore. MC sweep: "TreeStore clear slow large expanded tree reload", 10 results, all from the March build of #9404, no prior decision on the cost of clearing. Own-assignment sweep: 3 open, none overlapping.
Origin Session ID: dc38fc1a-5a51-4588-94b5-5467b1860850
Retrieval Hint: "TreeStore clear quadratic visible nodes reload splice indexOf"
🖖 Grace (Claude Opus 5.5, Claude Code) · session dc38fc1a-5a51-4588-94b5-5467b1860850
Context
Clearing a
TreeStoretakes time quadratic in its visible node count.Store#afterSetData()clears before it adds, so every data reload of a large flat or expanded tree pays the same cost.Measured on
dev5dcacbewith a unit probe (Turbo Mode, Node, ms):clear()/ reloadTreeStore, 10,000 flat visible nodesTreeStore, 20,000 flat visible nodesTreeStore, 40,000 flat visible nodesStore, 40,000 rows (control)TreeStore, 50 × 800 collapsed, 50 visible (control)TreeStore, 50 × 800 expanded, 40,050 visibleTreeStore, 40,000 flat,store.data = rowsThe Problem
TreeStore#splice()runs two O(N) scans once per removed node:me.indexOf(node) > -1.Collection#indexOf()is_items.indexOf(...), a scan of the projection.Collection#splice()as an array of items, whose per-item path runsitems.splice(me.indexOfKey(key), 1): a scan plus a shift for each item.clear()arrives as the numeric formsplice(0, count), and the override converts it to that array form (_items.slice(...)), so the Collection's one-pass numeric path is never taken.Each scan is quadratic on its own. At 40,000 flat visible nodes, fixing only the first leaves
clear()at 1,580 ms. Fixing only the second leaves 501 ms, still quadratic (52, 197, 501 ms at 10k, 20k, 40k). Fixing both gives 31 ms, and the reload drops from 1,532 to 85 ms.The Architectural Reality
src/data/TreeStore.mjsclear()wipes#allRecordsMapand#childrenMap, then callssuper.clear(). That reachesCollection#clear(), whosethis.splice(0, this.count)dispatches back into thesplice()override.splice()step 1b:if (me.indexOf(node) > -1) visibleToRemove.push(node). Step 4:super.splice(insertIndex, visibleToRemove, visibleToAdd, silent).me.map, holds exactly the visible keys (#rebuildKeysAndCount()rebuilds it from_items), andCollection#splice()removes by key.map.has(key)therefore answers the same question in O(1).TreeStore#addChildJSDoc, "Ingestion is already O(N) over a level, so a per-node key scan makes a bulk load O(N²) … in the one class whose whole premise is 50k-record ingestion."The Fix
In
TreeStore#splice():me.map.has(key)instead ofme.indexOf(node) > -1.index === 0 && removeCountOrToRemoveArray === me.count), delegate the numeric form,super.splice(0, me.count, visibleToAdd, silent), so the Collection drops the projection in one pass.Acceptance Criteria
clear()on aTreeStoreresolves no node position by scanning. A spec countsindexOf/indexOfKeycalls duringclear()of a flat and of an expanded tree and expects none; ondevit counts one or more per visible node.clear()and a data reload leave the state they leave ondevand fire the samemutatepayload: count 0, nothing reachable throughget()orgetChildren(), and the removed items in projection order.TreeStorespecs and the unit tier pass.Out of Scope
store.remove(manyNodes)). The Collection's array removal stays per-item, here and for plainStores; it gets its own ticket if measured to matter.me.indexOf(parentNode)check when adding under an expanded parent, which is not on the clear path.Related
TreeStore#addChild: the same class's bulk-load O(N²) fix, and the precedent for measuring at 20k+ nodes.clear()override against the split-brain leak. Its requirement thatclear()callssuper.clear()holds under this fix.Live latest-open + closed sweep: latest 20 open, plus open and closed searches for
TreeStore,TreeStore clearandTreeStore performance quadratic, at 2026-11-03T05:02Z. The same-surface closed hit is #9432 (Out of Scope / Avoided Traps read); no equivalent. A2A claim sweep: last 30 messages, all read states, no claim onTreeStore. MC sweep: "TreeStore clear slow large expanded tree reload", 10 results, all from the March build of #9404, no prior decision on the cost of clearing. Own-assignment sweep: 3 open, none overlapping.Origin Session ID: dc38fc1a-5a51-4588-94b5-5467b1860850 Retrieval Hint: "TreeStore clear quadratic visible nodes reload splice indexOf"
🖖 Grace (Claude Opus 5.5, Claude Code) · session dc38fc1a-5a51-4588-94b5-5467b1860850