mobx icon indicating copy to clipboard operation
mobx copied to clipboard

[Feature Request] Allow disabling or evicting of the descriptorCache

Open pcase-study opened this issue 7 months ago • 2 comments

The descriptorCache in observableObject.ts appears to trade a small amount of memory for performance when initializing an observable, creating one cached descriptor for every unique object key that mobx sees.

We encountered a situation where a library was using a plain java object as a Map, with UUIDs as the key. This wasn't causing any problems for end users, but on our SSR server the infinitely-many different UUIDs resulted in Mobx caching infinitely-many descriptors. This leaked memory with no way to recover it.

While we were able to patch the library to use an actual Map object to coax mobx into using ObservableMap instead of the ObservableObjectAdministration, it would be nice to have a way to either disable the descriptorCache entirely or have a utility method for clearing it.

pcase-study avatar May 28 '25 18:05 pcase-study

As suggested and documented, the correct way to model such keys is to use a Map indeed, not a plain object. Feel free to submit a PR with a configuration option to disable the descriptor cache, but it might be worth verifying first how negatively I'd effect your performance overall. So upstream fixing the offending package sounds like the best approach indeed.

mweststrate avatar Jun 02 '25 14:06 mweststrate

Hello, I'd like to know what the purpose of the descriptorCache is. Recently, I conducted performance tests with and without using the descriptorCache. With a total of 300,000 keys and 30,000 duplicate keys, the performance was better without using the descriptorCache, and the memory usage was also lower without it.

Sandbox Demo https://codesandbox.io/p/sandbox/ntrp45

Jlg1128 avatar Nov 10 '25 14:11 Jlg1128