performance pitfall: close over an object (instead properties) #76
Unanswered
tho-graf
asked this question in
General Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
we’re using the React Compiler in our new project (@ meisterplan.com), which has about 23k lines of code, and overall it’s working really well!
We did run into one issue: we accidentally closed over an entire context object that contains both state and update functions. In our case, we don’t actually read the state - we only call one of the functions.
When we close over the whole context, the compiler invalidates the cache even though the function we call hasn’t changed. This makes sense, since the compiler can’t know what we’re doing with the optimisticChanges object.
But when we pass only the specific function reference, everything works as expected and the cache is invalidated only when that function actually changes.
With useMemo, this was easy to see in the dependency array - but of course "painful". With the compiler, both versions look almost the same in code but behave very differently.
Do you think the compiler could improve its handling here? Or could a lint rule warn when a full context object is unnecessarily closed over?
Looking forward to your thoughts.
Beta Was this translation helpful? Give feedback.
All reactions