Add configure option to disable descriptor cache#4628
Add configure option to disable descriptor cache#4628anchmelev wants to merge 2 commits intomobxjs:mainfrom
Conversation
Add useDescriptorCache to configure so apps can opt out of descriptor reuse for observable plain-object keys. When disabled, MobX also clears the existing descriptor cache to avoid retaining descriptors for unbounded dynamic keys.
🦋 Changeset detectedLatest commit: 4b43010 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Gentle follow-up on this PR: would appreciate a review when someone has a chance. |
|
@anchmelev Thank you for the PR and digging into this. While I understand it can be a perf improvement I dont think that's a good direction. The issue here is that the observable plain objects are the wrong data structure for unbounded dynamic keys. We explicitly warn about that in the docs: https://mobx.js.org/observable-state.html
If we need to tell users "for this workload use some extra option to avoid trouble" then i'd rather just tell them to use observable.map instead of a plain object.
|
Thanks, that makes sense. I agree the core issue is using observable plain objects for unbounded dynamic keys, and I understand the concern about adding another niche escape hatch to I’ll drop this approach. |


This PR adds
useDescriptorCachetoconfigure.By default, MobX caches property descriptors for observable object keys so repeated keys can reuse the same getter/setter pair. That behavior stays unchanged.
The issue is that in workloads with effectively unbounded dynamic keys, such as UUID-based object dictionaries on SSR servers, the module-level
descriptorCachecan grow without bound and retain descriptors for keys that will never be reused.This change adds a configuration option to disable descriptor reuse for future observable object properties. When disabled, MobX also clears the existing descriptor cache so already accumulated entries can be released.
observable.mapis still the preferred model for truly dynamic key-based collections. This change is meant as an escape hatch for cases where switching data structures is not immediately possible.Code change checklist
/docs. AddeduseDescriptorCachetodocs/configuration.mdyarn mobx test:performance)Refs: #4555