Skip to content

[compiler] Add enableObjectIsComparison feature flag#609

Open
everettbu wants to merge 1 commit into
mainfrom
pr35894
Open

[compiler] Add enableObjectIsComparison feature flag#609
everettbu wants to merge 1 commit into
mainfrom
pr35894

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35894
Original author: josephsavona


Adds a feature flag that causes the compiler to emit !Object.is(a, b) instead of a !== b for dependency comparisons in memoized scopes. This matches the comparison semantics used by React's own hooks (useState, useMemo, etc.), correctly handling NaN values. To keep code compact, the actual output stores const _is = Object.is in module scope, and then emits comparisons as !_is(a, b).

Note this is an alternative to #35885 that emits more compact code and also avoids thrashing fixtures or changing default behavior. We can flip this to to the default behavior after testing.

Adds a feature flag that causes the compiler to emit `!Object.is(a, b)` instead of `a !== b` for dependency comparisons in memoized scopes. This matches the comparison semantics used by React's own hooks (useState, useMemo, etc.), correctly handling NaN values.
@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Feb 24, 2026
@greptile-apps

greptile-apps Bot commented Feb 24, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds a new enableObjectIsComparison feature flag to the React compiler that, when enabled, changes dependency comparisons in memoized scopes from a !== b to !Object.is(a, b). This aligns the compiler's comparison semantics with React's own hooks (useState, useMemo, etc.), which use Object.is internally. The key behavioral difference is NaN handling: NaN !== NaN is true (causing memos to never be reused for NaN values), while Object.is(NaN, NaN) is true (correctly treating NaN as unchanged).

  • Introduces a general-purpose addProgramConstant() mechanism in ProgramContext for hoisting module-level constants, used here to emit const is = Object.is (or _is if is is already in scope)
  • Adds codegenNegatedComparison() helper that replaces !== in three codegen sites: dependency checks, HMR hash comparisons, and early return sentinel comparisons
  • The flag defaults to false, preserving existing behavior until it can be validated in production
  • Includes four test fixtures covering the basic case, name collision handling, early return paths, and HMR interaction

Confidence Score: 5/5

  • This PR is safe to merge — it adds a feature flag defaulting to false with no impact on existing behavior.
  • The change is well-scoped and behind a feature flag that defaults to false, meaning zero impact on existing users. The implementation correctly uses the existing newUid mechanism for name collision avoidance, deduplicates the hoisted constant across multiple comparison sites, and covers all three codegen locations that emit !== comparisons. Test fixtures cover key edge cases (name collisions, early returns, HMR). The general-purpose addProgramConstant abstraction is a clean extension of the existing ProgramContext pattern.
  • No files require special attention.

Important Files Changed

Filename Overview
compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts Adds enableObjectIsComparison boolean config flag (default false) to EnvironmentConfigSchema with clear documentation about NaN handling semantics.
compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Imports.ts Adds constants map and addProgramConstant() method to ProgramContext for hoisting program-level constants (e.g., const is = Object.is), with deduplication and unique name generation. Constants are emitted after imports in addImportsToProgram.
compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts Adds codegenNegatedComparison() helper and replaces three !== comparison sites (dependency check, HMR hash check, early return sentinel check) to conditionally use !Object.is(a, b) when the flag is enabled.

Last reviewed commit: bc50ab4

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

11 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants