Anurag’s Read

The uphill climb of making diff lines performant.

A solid front-end performance case with measured before and after numbers. It shows how to cut components, state and handlers, and when to add virtualization only for the worst cases.
Score
8.6
Verdict
Must read
Blog and date
GitHub,
Original length and level
9 min, intermediate
Topics
frontend, performance, react, virtualization
Read the original

Context

The Files changed tab runs in React and is now the default for all users. On extreme pull requests the JavaScript heap passed 1 GB and DOM nodes passed 400,000. Interaction latency (INP) became unacceptable as diffs grew.

The v1 design used many small shared components close to the old Rails view.

Architecture

The v1 diff line used about 10 DOM elements in unified view and 15 in split view. It also used at least 8 or 13 React components per line, each with several event handlers. The v2 design uses separate unified and split components, so each carries only its own logic.

A single top-level handler reads data attributes to find the target line. Comment and menu state live in children that render only when needed. Very large diffs use virtualization so only visible rows exist in the DOM.

Mechanisms

M1

Event delegation

One handler near the top reads data attributes from the event target. It replaces more than 20 handlers per line. Drag selection uses the same path to highlight lines.

M2

Lookup maps

Selectors use JavaScript Maps keyed by file path and line. This gives constant-time access to comments and selection state. It removes scans over nested arrays.

M3

Effect rules

useEffect is allowed only at the top level of a diff file. A lint rule blocks it in line components. This lets line components be memoized safely.

M4

Virtualization

Applies only to pull requests above about 10,000 diff lines. TanStack Virtual swaps rows as the user scrolls. Smaller pull requests keep native find-in-page.

Design decisions

No.ChoseInstead ofWhy
D1Separate split and unified componentsShared thin wrappers for both viewsThe wrappers carried unused branches, and the team accepted some duplicated code.
D2Virtualize only the largest pull requestsVirtualize everythingWorst-case fixes can hurt everyday reviews, such as native search.

Key numbers

  • Components rendered fell from about 183,504 to about 50,004.
  • INP fell from about 450 ms to about 100 ms.
  • Memory fell from about 150 to 250 MB to about 80 to 120 MB.
  • DOM nodes fell only about 10%, from 200,000 to 180,000.
  • Virtualized INP fell from 275 to 700 ms to 40 to 80 ms.

Failures and limits

  • Cost grew linearly with pull request size in v1.
  • The results come from one 10,000-line diff on one device profile.
  • The post does not say how virtualization affects find-in-page for the largest diffs.
  • Separate view components duplicate some code.

Lessons you can use

  • Count components and handlers per row, because they multiply with list size.
  • Colocate rare state in children so most rows carry none.
  • Use a tiered approach, with heavy tricks only for the extreme tail.

Glossary

INPInteraction to Next Paint, a measure of how fast a page responds to input.
VirtualizationRendering only the rows currently visible in a long list.