Skip to content

fix(compiler): use Object.is() for cache dependency comparison to handle NaN#602

Closed
everettbu wants to merge 2 commits into
mainfrom
fix/compiler-nan-cache-comparison
Closed

fix(compiler): use Object.is() for cache dependency comparison to handle NaN#602
everettbu wants to merge 2 commits into
mainfrom
fix/compiler-nan-cache-comparison

Conversation

@everettbu

Copy link
Copy Markdown

Mirror of facebook/react#35885
Original author: fresh3nough


Fixes #35854

Summary

The React Compiler generates cache dependency checks using strict inequality (!==), but this produces incorrect behavior for NaN values since NaN !== NaN is always true in JavaScript. This causes memoized values with NaN dependencies to be recomputed on every render, breaking the memoization guarantee.

React's runtime hook dependency comparison (in areHookInputsEqual) uses Object.is() semantics, which correctly handles NaN (Object.is(NaN, NaN) returns true). The compiler-generated code should be consistent with this behavior.

Changes

CodegenReactiveFunction.ts: Changed the dependency comparison codegen from $[n] !== dep to !Object.is($[n], dep), which correctly handles both NaN and -0/+0 edge cases, matching the runtime's Object.is semantics.

Before (generated code):

if ($[0] !== dep) { ... }

After (generated code):

if (!Object.is($[0], dep)) { ... }

How to reproduce

Create a component where a dependency evaluates to NaN:

function Component({ value }) {
  const derived = 0 / 0; // NaN
  const result = useMemo(() => [derived], [derived]);
  return result;
}

Before this fix, the memoized value would be recomputed on every render because NaN !== NaN is always true.

Test plan

  • Added cache-nan-dependency fixture that compiles a component with a NaN dependency
  • Updated all 859 existing fixture snapshots to reflect the new Object.is() comparison
  • Ran yarn prettier and yarn linc with no issues

@greptile-apps

greptile-apps Bot commented Feb 23, 2026

Copy link
Copy Markdown

Too many files changed for review. (862 files found, 500 file limit)

@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Feb 24, 2026
@everettbu
everettbu deleted the fix/compiler-nan-cache-comparison branch February 24, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants