LearnNewsExamplesServices
Frontmatter
id19192
titleStore#forEach and Collection#findBy copy all items on every iteration
stateClosed
labels
bugaiperformancecore
assigneesneo-opus-ada
createdAtSep 24, 2026, 9:05 PM
updatedAtSep 24, 2026, 9:56 PM
githubUrlhttps://github.com/neomjs/neo/issues/19192
authorneo-opus-ada
commentsCount0
parentIssuenull
subIssues[]
subIssuesCompleted0
subIssuesTotal0
contentTrust
projected
quarantined0
signals[]
blockedBy[]
blocking[]
closedAtSep 24, 2026, 9:56 PM

Store#forEach and Collection#findBy copy all items on every iteration

Closed v13.2.0/archive-v13-2-0-chunk-23 bugaiperformancecore
neo-opus-ada
neo-opus-ada commented on Sep 24, 2026, 9:05 PM

Context

I found this while profiling Memory Core's mark_read drain today (neo-agent-brain#464). 6.6 GB of that drain's allocation sat in the generic config getter's legacy array copy.

The engine's own collections read the same getter. collection.Base declares items_ as {clone: 'shallow', value: null} without a cloneOnGet, so every collection.items read returns a fresh copy. Measured: c.items !== c.items. One read of a 100k collection costs about 0.067 ms, against about 0.00013 ms for getCount().

The Problem

Two core loops read that getter once per iteration, so their cost grows quadratically:

  • Neo.collection.Base#findBy reads me.items[i] inside its loop, twice on a match.
  • Neo.data.Store#forEach passes me.items as the callback's fourth argument on every iteration.

Measured on dev@202699d10c with a unit probe whose callback does nothing:

n Collection#findBy Store#forEach plain getAt loop
1,000 1 ms 1 ms 0 ms
10,000 13 ms 13 ms 2 ms
50,000 1,802 ms 1,876 ms 10 ms

The engine's callers are list.Base (while rendering), tree.List (three sites), tree.Accordion (two), and the list and tree drag zones. It is invisible at today's typical sizes, but at 50k records a single call blocks the App worker for about 1.9 s.

The Architectural Reality

  • The generated config getter in src/Neo.mjs has no cloneOnGet for items, so it takes the legacy branch: Array.isArray(value) leads to value.slice().
  • collection.Base already reads its backing array as me._items wherever it must not copy, 22 times in all.
  • data.Store#findBy delegates to super.findBy and maps the results to Records.

The Fix

  • Collection#findBy: iterate the backing _items array, not the getter.
  • Store#forEach: take one copy of items before the loop and pass it on every call. The fourth argument stays a defensive copy, but there is now one copy per call instead of one per record.
  • Red-first unit cases count items getter reads per call, so the test does not rely on timings.

Decision Record impact: none.

Acceptance Criteria

  • AC-1 A unit case shows that Collection#findBy over n items reads the items getter zero times, and that Store#forEach reads it once per call. Both cases are red on dev (n reads).
  • AC-2 Rerun on the 50k probe, findBy and forEach each finish within 5× of the plain getAt loop. The receipt goes in the PR.
  • AC-3 The full unit suite is green.

Out of Scope

  • The getter's copy itself. Giving items a non-copying cloneOnGet changes a public contract, since callers may rely on a copy they can mutate.
  • Single reads such as me.items.length, which cost O(n) per call, not per iteration.

Related

neo-agent-brain#464 and neo-agent-brain#467 cover the same copy class in Memory Core's graph store.

Sweeps (at filing):

  • Live latest-open: latest 20 open issues, none equivalent. A keyword search over open issues found nothing.
  • A2A: no claim on this scope.
  • MC: the February 2026 sessions switched items_ to a shallow clone on set but left these loops as they were.
  • Own assignments: none on this surface.

Origin Session ID: 101d2ce9-9f43-4f5a-9ae2-3c75bf8f6fcf Retrieval Hint: "collection items getter copies per read quadratic findBy Store forEach cloneOnGet"

Contract Ledger (added for PR #19193's review)

Target surface Source of authority Behavior after the fix Fallback Docs Evidence
Neo.collection.Base#findBy(fn, scope, start, end) the method Iterates the backing _items, re-read on every iteration, so the collection's current state stays visible to the callback. It returns the matching items as before. — JSDoc unchanged MassiveData.spec.mjs: 0 getter reads per call (110 on dev)
Neo.data.Store#forEach(fn, scope), called back as fn(record, index, items) the method items is one defensive copy, shared by all the callbacks of a call: (1) every entry the loop has reached is its Record (items[index] === record, lazy hydration included); (2) a callback that changes the store's count gets a fresh copy from the next call on; (3) mutating items never reaches the store. — JSDoc on forEach ("treat it as read-only") Store.spec.mjs "forEach callback items": 3 cases, green on dev, red on a plain one-copy version. MassiveData.spec.mjs: 1 getter read per call when the count is unchanged.

The one intentional change from dev: dev gave every call its own fresh copy, so a callback mutating items only affected its own copy. Now the copy is shared across calls, so treat it as read-only.

tobiu referenced in commit 43bdac9 - "fix(data): findBy and forEach stop copying all items per iteration (#19192) (#19193) on Sep 24, 2026, 9:56 PM
tobiu closed this issue on Sep 24, 2026, 9:56 PM