aweary / react-copy-write

✍️ Immutable state with a mutable API
MIT License
1.79k stars 54 forks source link

[WIP] Implement createSelector for optimized selectors #9

Closed aweary closed 5 years ago

aweary commented 6 years ago

This adds a new API, createSelector, that lets you define optimizable selectors. These selectors can be optimized using the unstable_observedBits React context API. This optimization strategy assumes that selectors, not consumers, are the basic abstraction for composability; since selectors can be re-used between consumers optimizing at the selector level is inherently more powerful.

These selectors are optimized using a heuristic based on the reference count for a given selector; the first 29 selectors used in mounted consumers are always optimized. After that, they get queued up for optimization. Once an optimized selector can be de-optimized (all the consumers using it unmount) it frees up a slot, and the queued selector with the highest reference count is optimized.

cc @gaearon

TODO