Make `detachDeletedInstance` 30% faster
According to how JS works, null instead of delete keyword is faster from 2x to 4-5x depending on a browser engine.
I reviewed the usage of these fields across the codebase and everywhere we check if the key is undefined or null, so the change is safe. All tests passed.
The main benefit - it speeds up user interactions, because (at least in my case) this method runs synchronously when I type in an input.
Summary
- Replace uses of delete obj[key] with obj[key] = null in the
detachDeletedInstancefunction - Motivation: delete changes object shape (hidden class) and deoptimizes inline caches.
- Semantics: I reviewed the usage of the keys, we don't do
key in objfor these keys across the codebase. - No public API changed (given the keys are not a part of the public api)
On Chrome/Firefox/Safari locally, the null-assignment loop was consistently faster (roughly 2–5x depending on engine/version), matching expectations from hidden-class/IC behavior.
How did you test this change?
- yarn test
- tests/perf benches in the chrome devtools, confirming the improvement
- All tests passed locally.
Comparison
Methodology:
- React in prod mode
- Without React devtools
- 6x slowdown in chrome devtools
- Macbook Pro M1 max
Before:
After:
Hi @MrFlashAccount!
Thank you for your pull request and welcome to our community.
Action Required
In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.
Process
In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.
Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.
If you have received this in error or have any questions, please contact us at [email protected]. Thanks!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
Maybe someone from the react core team could take a look at this PR? @eps1lon , @rickhanlonii , @acdlite ? 🙏