Skip to content

perf: look up each model property once in getSnapshot - #2307

Open
lavrton wants to merge 1 commit into
mobxjs:masterfrom
lavrton:perf/snapshot-single-lookup
Open

lavrton wants to merge 1 commit into
mobxjs:masterfrom
lavrton:perf/snapshot-single-lookup

Conversation

@lavrton

@lavrton lavrton commented Oct 15, 2026

Copy link
Copy Markdown

What does this PR do and why?

For each property, ModelType.getSnapshot called getAtom(storedValue, name) to report it observed, then getChildNode, which calls _getAdministration and so getAtom again. Both return the same ObservableValue: every model property is an observable.ref on the instance. With MobX 7 each lookup costs several property reads.

This PR reads the child node with raw() from the first lookup, as getChildNode does. The "Node not available" guard stays. Dependencies and snapshot values do not change.

Benchmark: store with 10,000 todos (5 properties each).

workload master this PR
toggle done on all 10k todos (one action) 60.7 ms 50.5 ms (-17%)
toggle all, then getSnapshot(store) 60.5 ms 51.2 ms (-15%)
  • New test Model snapshots > getSnapshot observes each property it reads: a reaction on getSnapshot(m) must fire when a primitive changes and when a child model is replaced. It fails if reportObserved() is removed or raw() is replaced by get(). It also passes on master (pure performance change).

ModelType.getSnapshot looked up each property's observable twice: once with
getAtom to report it observed, and again through getChildNode. Both return
the same ObservableValue. Read the child node with raw() from the first
lookup instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant