react icon indicating copy to clipboard operation
react copied to clipboard

Make `detachDeletedInstance` 30% faster

Open MrFlashAccount opened this issue 2 months ago • 3 comments

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 detachDeletedInstance function
  • 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 obj for 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:

CleanShot 2025-11-21 at 14 19 44@2x

After:

CleanShot 2025-11-21 at 14 20 52@2x

MrFlashAccount avatar Oct 30 '25 08:10 MrFlashAccount

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!

meta-cla[bot] avatar Oct 30 '25 08:10 meta-cla[bot]

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

meta-cla[bot] avatar Nov 21 '25 11:11 meta-cla[bot]

Maybe someone from the react core team could take a look at this PR? @eps1lon , @rickhanlonii , @acdlite ? 🙏

MrFlashAccount avatar Dec 01 '25 14:12 MrFlashAccount