Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท Performance and Patterns

Rendering Performance and sx Cost

Make Material UI screens fast: the real cost of the sx prop, stable theme and style objects, React.memo, avoiding re-render storms, and list virtualization.

Material UI components do real work: they compute styles, merge the theme, create class names and add event handling. For most screens this is invisible. But when a page renders thousands of cells, or something near the top of the tree re-renders many times a second (typing into a field, dragging a slider), the cost shows up as lag. This lesson explains what costs what and shows the fixes that matter most.

MUI is not available in the live editors. Real MUI code is read-only. The runnable examples are stand-ins that count renders and measure work, to show the same effects without the library.

Measure before you fix

The React DevTools Profiler shows which components rendered and why. Switch on "Record why each component rendered" and record the interaction that feels slow. Look for:

  • a component that renders on every keystroke but does not depend on the typed value,
  • the same component rendering hundreds of times in a list,
  • long "commits" (more than about 16 ms) while a user is typing or scrolling.

Fix what the profile shows, not what you suspect.

What the sx prop costs

The sx prop is convenient and compiles to CSS classes through Emotion. Each use runs a function that:

  1. resolves the theme shortcuts (p, bgcolor, 'primary.main'),
  2. serializes the resulting styles to a string,
  3. hashes it to a class name, and inserts a rule if that class is new.

This is a few microseconds per element, so a hundred sx uses are fine. At 5,000 table cells, the cost adds up, and the problem gets worse when the sx object is a new object on every render, which also breaks React.memo for any child that receives it.

Practical rules:

  • For static styles on repeated elements, use a styled component or a plain CSS class; the style is computed once per component, not per element.
  • Keep the object stable: define it outside the component, or useMemo it if it depends on props.
  • Prefer a few sx props higher up (on a container with '& .cell': {...}) over one on every child.
  • Do not generate styles dynamically with many distinct values (a different bgcolor hex for each of 10,000 rows creates 10,000 class rules). Use a CSS variable set through style instead:
JSX
// Real Material UI, read-only
const Cell = styled('td')({ backgroundColor: 'var(--cell-bg)' });

rows.map((row) => <Cell key={row.id} style={{ '--cell-bg': row.color }} />);

This creates one class and sets a variable per cell.

Try it: inline objects break memoization

React.memo skips a re-render when the props are shallow-equal. A fresh object literal in each render is never equal to the previous one. Type in the box: the "inline sx" row list re-renders every time, the "stable sx" list does not.

Stable vs inline style objects with React.memo

(The counters are read during render, so they lag by one keystroke in the displayed text. That is fine for the demonstration.)

Create the theme once

A new createTheme(...) object inside a component tells every consumer that the theme changed. All styled components below the provider recompute their styles. Create the theme at module level; if it depends on state (like a dark-mode choice), use useMemo, or better, use colorSchemes with CSS variables so that mode changes do not re-render the tree (see Dark Mode and Color Schemes).

Stop unnecessary re-renders

Typing into a controlled TextField re-renders the component that owns the state. If that is a big form or page, everything under it re-renders on every keystroke. Options, in the order to try them:

  1. Push state down. Move the input and its state into a small component, so only it re-renders.
  2. Memoize expensive children with React.memo and keep their props stable (useCallback for handlers, useMemo for objects).
  3. Use uncontrolled inputs or a form library (React Hook Form) that does not re-render the whole form on each key.
  4. Debounce the work that follows typing (search requests, filtering), with useDeferredValue or a timer.
JSX
// Real Material UI, read-only
function SearchBox({ onSearch }) {
  const [value, setValue] = useState(''); // state lives here, not in the page
  const deferred = useDeferredValue(value);
  useEffect(() => { onSearch(deferred); }, [deferred, onSearch]);
  return <TextField label="Search" value={value} onChange={(e) => setValue(e.target.value)} />;
}

Try it: pushing state down

State in the page vs state in the input

Long lists: virtualization

If a list can hold hundreds or thousands of rows, do not render them all. Virtualization renders only the rows in view plus a small buffer. Options: react-window or @tanstack/react-virtual for custom lists, and the MUI X Data Grid for tables (it virtualizes rows and columns by default). The pattern with MUI lists:

JSX
// Real Material UI + react-window, read-only
import { FixedSizeList } from 'react-window';

function Row({ index, style, data }) {
  return (
    <ListItem style={style} key={index} component="div" disablePadding>
      <ListItemButton><ListItemText primary={data[index].name} /></ListItemButton>
    </ListItem>
  );
}

<FixedSizeList height={400} width="100%" itemSize={46} itemCount={data.length} itemData={data}>
  {Row}
</FixedSizeList>

For long selects, Autocomplete can virtualize its listbox with a custom ListboxComponent.

Try it: a minimal virtual list

This stand-in renders 100,000 rows but only about ten at a time. Scroll the box and watch the DOM row count stay small.

Windowing 100,000 rows

Other quick wins

  • Avoid heavy transitions on big lists; set TransitionProps={{ unmountOnExit: true }} or transitionDuration={0} for list items where animation adds nothing.
  • Disable the ripple (disableRipple) in theme defaultProps on dense UIs.
  • Use Tooltip sparingly in lists; each one wraps its child and adds listeners. Add it only to elements that need it.
  • Prefer Grid/Stack/Box for layout; they are light. Avoid deep nesting of wrappers purely for spacing.
  • Production builds only. The development build of React and Emotion is much slower; do not benchmark it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can an inline sx={{ ... }} object defeat React.memo on a child?

    memo compares props with Object.is. A fresh object literal is never equal to the previous one.
  2. 2. What is the first thing to try when typing in one input makes a large page lag?

    Pushing state down limits how much re-renders on each keystroke, often removing the problem without memo.
  3. 3. How do you give 10,000 table cells different background colors without creating 10,000 CSS classes?

    A CSS variable lets one class serve every cell. Per-cell sx would generate a new class for every distinct value.
  4. 4. What does list virtualization do?

    Only visible rows (plus a buffer) are in the DOM, so a huge list costs about the same as a small one.

Summary

  • Profile first. The React DevTools Profiler shows what renders and why.
  • sx is fine at normal scale; for repeated elements use styled or CSS, keep objects stable, and use CSS variables for many distinct values.
  • Create the theme once; use colorSchemes and CSS variables so a mode switch does not re-render the tree.
  • Push state down, memoize expensive children with stable props, and debounce follow-up work.
  • Virtualize long lists; use the Data Grid for big tables.

Next: Composition and Custom Components