Sub topic 3
Rendering Only What Is Visible: List Virtualization
Assume you know JavaScript functions and arrays, React function components, props, state, hooks, and basic browser developer tools. This chapter focuses on measuring and reducing work after a component already renders correctly.
Rendering only what is visible: list virtualization
A long list can create thousands of DOM nodes even though the user sees only a few rows. List virtualization renders only the visible rows plus a small buffer. As the user scrolls, the application reuses those rows for new items.
A virtualization library usually needs the list height, row height, item count, and a row-rendering function. Keep row keys stable, and avoid putting expensive calculations inside every row.
Keeping input responsive with useDeferredValue and useTransition
useDeferredValue lets urgent state, such as input text, update before slower results. useTransition marks a state update as non-urgent and exposes a pending status. Both can keep interaction responsive, but they do not reduce the total work.
import { useDeferredValue, useState } from "react";
export function Search({ items }) {
const [query, setQuery] = useState("");
const deferredQuery = useDeferredValue(query);
const results = items.filter((item) =>
item.name.toLowerCase().includes(deferredQuery.toLowerCase())
);
return (
<>
<input value={query} onChange={(event) => setQuery(event.target.value)} />
<p>{results.length} results</p>
</>
);
}Measuring in code with the Profiler component
React’s Profiler calls a callback after its subtree commits. actualDuration reports the time for the current update. baseDuration estimates the cost without memoization.
import { Profiler } from "react";
function onRender(id, phase, actualDuration, baseDuration) {
console.log(id, phase, {
actual: actualDuration.toFixed(1),
base: baseDuration.toFixed(1),
});
}
export function App() {
return (
<Profiler id="Results" onRender={onRender}>
<Results />
</Profiler>
);
}Going beyond React: the browser Performance panel
Use the browser Performance panel when React timings do not explain the delay. Record a user interaction and inspect:
- Long tasks on the main thread.
- Layout, style recalculation, and painting.
- JavaScript outside React.
- Network and resource timing.
A large script or image can delay startup even when React renders quickly.
Reducing load cost with code splitting
Code splitting moves code into separate bundles. React.lazy loads a component when it is first rendered.
import { lazy, Suspense } from "react";
const Chart = lazy(() => import("./Chart"));
export function Report() {
return (
<Suspense fallback={<p>Loading chart...</p>}>
<Chart />
</Suspense>
);
}Verifying improvements and guarding against regressions
Record the same interaction before and after each change. Compare commit time, input latency, long tasks, DOM node count, and initial bundle size. Test a realistic data set on a slower device. Keep performance tests or profiler recordings for important screens.
Exercise
A search page has 20,000 rows, and typing feels slow. Which changes should you investigate first?
Show answer
Measure the interaction, virtualize the list, and defer the expensive filtering or results update. Then compare the same interaction before and after the changes.