Skip to content

[all components] Drop unnecessary memoization - #4693

Merged
flaviendelangle merged 5 commits into
mui:masterfrom
flaviendelangle:useless-memoization
Apr 28, 2026
Merged

flaviendelangle merged 5 commits into
mui:masterfrom
flaviendelangle:useless-memoization

Conversation

@flaviendelangle

Copy link
Copy Markdown
Member

useRenderElement reads its props/state/ref only during render — it has no useMemo/useEffect/useCallback internally — so referential stability of values passed to it is never required. mergeProps always returns fresh objects too. Likewise, a useStableCallback whose only consumer is inside another useStableCallback's body, or inside a JSX-inline event handler, is dead weight: the outer wrapper (or fresh JSX render) already provides freshness.

Decision rule applied per wrapper: KEEP if consumed by a context Provider's value, another hook's deps array, an effect, an event listener stored imperatively, an external store, or useImperativeHandle. DROP otherwise.

Switch

  • SwitchRoot: drop inputProps useMemo (only spread on a hidden ) and useStableCallback(onCheckedChangeProp) (only fed the dead useMemo).

Checkbox

  • CheckboxRoot: drop useStableCallback(onCheckedChangeProp); only call site is inline in the input's onChange handler.
  • CheckboxIndicator: drop stateAttributesMapping useMemo; only consumer is useRenderElement.

Radio

  • RadioRoot: drop serializedValue useMemo; only consumer is the input's value prop. Inlined as serializeValue(value).

Toggle

  • Toggle: drop useStableCallback(onPressedChangeProp); body inlined into the JSX-inline onClick handler (re-bound each render).

ToggleGroup

  • ToggleGroup: drop defaultValue useMemo; useControlled compares defaults by serialization, not identity. Inlined with EMPTY_ARRAY for stability.

Progress

  • ProgressIndicator: drop getStyles useCallback; the function was invoked inline (style: getStyles()) so its identity was never observed.

Slider

  • SliderThumb: replace getThumbStyle useCallback with a let thumbStyle block; same reason as ProgressIndicator.
  • SliderValue: drop outputFor and defaultDisplayValue useMemos; only consumed in the JSX prop/children passed to useRenderElement.

Popover

  • PopoverTrigger: drop stateAttributesMapping useMemo; only consumer is useRenderElement.

Tabs

  • TabsList: drop notifyIndicatorUpdateListeners useStableCallback; only consumer is the body of a single useEffect, which can capture the ref set directly without identity stability.
  • TabsIndicator: drop activeTabPosition, activeTabSize, and style useMemos; all three are consumed only by useRenderElement.

Accordion

  • AccordionRoot: drop useStableCallback(onValueChangeProp); only consumer is the body of handleValueChange (also a useStableCallback), whose trampoline already re-binds the latest closure each render.
  • AccordionPanel: drop panelState useMemo; only consumed by resolveStyle and useRenderElement.
  • AccordionTrigger: drop props useMemo; only passed to useRenderElement.

Collapsible

  • CollapsibleTrigger: drop props useMemo; only passed to useRenderElement.
  • CollapsiblePanel: drop panelState useMemo; same shape as AccordionPanel.

Menu

  • MenuCheckboxItem: drop handleClick useStableCallback; only used as the JSX-inline onClick handler (re-bound each render).
  • MenuLinkItem: drop state useMemo; only consumed by useRenderElement.
  • MenuRadioItem: same as MenuCheckboxItem.

Select

  • SelectTrigger: drop ariaControlsId useMemo; only consumed in the aria-controls JSX prop.

Drawer

  • DrawerRoot: drop useStableCallback(onSnapPointChangeProp); only consumer is the body of setActiveSnapPoint (also a useStableCallback).

useRenderElement reads its props/state/ref only during render — it has no
useMemo/useEffect/useCallback internally — so referential stability of values
passed to it is never required. mergeProps always returns fresh objects too.
Likewise, a useStableCallback whose only consumer is inside another
useStableCallback's body, or inside a JSX-inline event handler, is dead
weight: the outer wrapper (or fresh JSX render) already provides freshness.

Decision rule applied per wrapper: KEEP if consumed by a context Provider's
value, another hook's deps array, an effect, an event listener stored
imperatively, an external store, or useImperativeHandle. DROP otherwise.

Switch
- SwitchRoot: drop inputProps useMemo (only spread on a hidden <input>) and
  useStableCallback(onCheckedChangeProp) (only fed the dead useMemo).

Checkbox
- CheckboxRoot: drop useStableCallback(onCheckedChangeProp); only call site
  is inline in the input's onChange handler.
- CheckboxIndicator: drop stateAttributesMapping useMemo; only consumer is
  useRenderElement.

Radio
- RadioRoot: drop serializedValue useMemo; only consumer is the input's
  value prop. Inlined as serializeValue(value).

Toggle
- Toggle: drop useStableCallback(onPressedChangeProp); body inlined into
  the JSX-inline onClick handler (re-bound each render).

ToggleGroup
- ToggleGroup: drop defaultValue useMemo; useControlled compares defaults
  by serialization, not identity. Inlined with EMPTY_ARRAY for stability.

Progress
- ProgressIndicator: drop getStyles useCallback; the function was invoked
  inline (style: getStyles()) so its identity was never observed.

Slider
- SliderThumb: replace getThumbStyle useCallback with a let thumbStyle
  block; same reason as ProgressIndicator.
- SliderValue: drop outputFor and defaultDisplayValue useMemos; only
  consumed in the JSX prop/children passed to useRenderElement.

Popover
- PopoverTrigger: drop stateAttributesMapping useMemo; only consumer is
  useRenderElement.

Tabs
- TabsList: drop notifyIndicatorUpdateListeners useStableCallback; only
  consumer is the body of a single useEffect, which can capture the ref
  set directly without identity stability.
- TabsIndicator: drop activeTabPosition, activeTabSize, and style
  useMemos; all three are consumed only by useRenderElement.

Accordion
- AccordionRoot: drop useStableCallback(onValueChangeProp); only consumer
  is the body of handleValueChange (also a useStableCallback), whose
  trampoline already re-binds the latest closure each render.
- AccordionPanel: drop panelState useMemo; only consumed by resolveStyle
  and useRenderElement.
- AccordionTrigger: drop props useMemo; only passed to useRenderElement.

Collapsible
- CollapsibleTrigger: drop props useMemo; only passed to useRenderElement.
- CollapsiblePanel: drop panelState useMemo; same shape as AccordionPanel.

Menu
- MenuCheckboxItem: drop handleClick useStableCallback; only used as the
  JSX-inline onClick handler (re-bound each render).
- MenuLinkItem: drop state useMemo; only consumed by useRenderElement.
- MenuRadioItem: same as MenuCheckboxItem.

Select
- SelectTrigger: drop ariaControlsId useMemo; only consumed in the
  aria-controls JSX prop.

Drawer
- DrawerRoot: drop useStableCallback(onSnapPointChangeProp); only consumer
  is the body of setActiveSnapPoint (also a useStableCallback).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@flaviendelangle flaviendelangle self-assigned this Apr 27, 2026
@flaviendelangle flaviendelangle added performance scope: all components Widespread work has an impact on almost all components. labels Apr 27, 2026
@pkg-pr-new

pkg-pr-new Bot commented Apr 27, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4693
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4693
    

commit: f5ad2b2

@code-infra-dashboard

code-infra-dashboard Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-615B(-0.13%) ▼-214B(-0.15%)

Details of bundle changes

Performance

Total duration: 1,273.23 ms ▼-254.64 ms(-16.7%) | Renders: 53 (+0) | Paint: 1,982.55 ms ▼-402.81 ms(-16.9%)

Test Duration Renders
Popover mount (300 instances) 94.81 ms ▼-42.28 ms(-30.8%) 2 (+0)
Select mount (200 instances) 133.75 ms ▼-38.12 ms(-22.2%) 3 (+0)
Slider mount (300 instances) 142.74 ms ▼-25.75 ms(-15.3%) 3 (+0)
Checkbox mount (500 instances) 66.52 ms ▼-20.15 ms(-23.2%) 1 (+0)
Dialog mount (300 instances) 81.36 ms ▼-19.68 ms(-19.5%) 2 (+0)

...and 7 more. View full report

Details of benchmark changes


Check out the code infra dashboard for more information about this PR.

const [groupValue, setValueState] = useControlled({
controlled: valueProp,
default: defaultValue,
default: valueProp === undefined ? (defaultValueProp ?? EMPTY_ARRAY) : undefined,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We have a similar pattern with useMemo on the accordion with a comment I fail to understand 😆
So not sure if we want to drop the memoization here.

cc @atomiks

@atomiks atomiks Apr 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Accordion one returns a non-stable array (but maybe not intentionally) so it has to stay memoized unless replaced with EMPTY_ARRAY

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, we would need to use EMPTY_ARRAY before dropping memoization.
But I assumes something smarter was happening that was worth writting a comment 😆

@netlify

netlify Bot commented Apr 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit f5ad2b2
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69f055fe3c78410008e59ebb
😎 Deploy Preview https://deploy-preview-4693--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@flaviendelangle
flaviendelangle marked this pull request as ready for review April 27, 2026 14:46
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Apr 28, 2026
flaviendelangle and others added 2 commits April 28, 2026 08:09
useRenderElement reads its props/state/ref only during render — it has no
useMemo/useEffect/useCallback internally — so referential stability of values
passed to it is never required. mergeProps always returns fresh objects too.
Likewise, a useStableCallback whose only consumer is inside another
useStableCallback's body, or inside a JSX-inline event handler, is dead
weight: the outer wrapper (or fresh JSX render) already provides freshness.

Decision rule applied per wrapper: KEEP if consumed by a context Provider's
value, another hook's deps array, an effect, an event listener stored
imperatively, an external store, or useImperativeHandle. DROP otherwise.

Switch
- SwitchRoot: drop inputProps useMemo (only spread on a hidden <input>) and
  useStableCallback(onCheckedChangeProp) (only fed the dead useMemo).

Checkbox
- CheckboxRoot: drop useStableCallback(onCheckedChangeProp); only call site
  is inline in the input's onChange handler.
- CheckboxIndicator: drop stateAttributesMapping useMemo; only consumer is
  useRenderElement.

Radio
- RadioRoot: drop serializedValue useMemo; only consumer is the input's
  value prop. Inlined as serializeValue(value).

Toggle
- Toggle: drop useStableCallback(onPressedChangeProp); body inlined into
  the JSX-inline onClick handler (re-bound each render).

ToggleGroup
- ToggleGroup: drop defaultValue useMemo; useControlled compares defaults
  by serialization, not identity. Inlined with EMPTY_ARRAY for stability.

Progress
- ProgressIndicator: drop getStyles useCallback; the function was invoked
  inline (style: getStyles()) so its identity was never observed.

Slider
- SliderThumb: replace getThumbStyle useCallback with a let thumbStyle
  block; same reason as ProgressIndicator.
- SliderValue: drop outputFor and defaultDisplayValue useMemos; only
  consumed in the JSX prop/children passed to useRenderElement.

Popover
- PopoverTrigger: drop stateAttributesMapping useMemo; only consumer is
  useRenderElement.

Tabs
- TabsList: drop notifyIndicatorUpdateListeners useStableCallback; only
  consumer is the body of a single useEffect, which can capture the ref
  set directly without identity stability.
- TabsIndicator: drop activeTabPosition, activeTabSize, and style
  useMemos; all three are consumed only by useRenderElement.

Accordion
- AccordionRoot: drop useStableCallback(onValueChangeProp); only consumer
  is the body of handleValueChange (also a useStableCallback), whose
  trampoline already re-binds the latest closure each render.
- AccordionPanel: drop panelState useMemo; only consumed by resolveStyle
  and useRenderElement.
- AccordionTrigger: drop props useMemo; only passed to useRenderElement.

Collapsible
- CollapsibleTrigger: drop props useMemo; only passed to useRenderElement.
- CollapsiblePanel: drop panelState useMemo; same shape as AccordionPanel.

Menu
- MenuCheckboxItem: drop handleClick useStableCallback; only used as the
  JSX-inline onClick handler (re-bound each render).
- MenuLinkItem: drop state useMemo; only consumed by useRenderElement.
- MenuRadioItem: same as MenuCheckboxItem.

Select
- SelectTrigger: drop ariaControlsId useMemo; only consumed in the
  aria-controls JSX prop.

Drawer
- DrawerRoot: drop useStableCallback(onSnapPointChangeProp); only consumer
  is the body of setActiveSnapPoint (also a useStableCallback).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…s-memoization

# Conflicts:
#	packages/react/src/menu/checkbox-item/MenuCheckboxItem.tsx
#	packages/react/src/menu/radio-item/MenuRadioItem.tsx
#	packages/react/src/progress/indicator/ProgressIndicator.tsx
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Apr 28, 2026
@flaviendelangle
flaviendelangle merged commit 27b08e8 into mui:master Apr 28, 2026
7 checks passed
@flaviendelangle
flaviendelangle deleted the useless-memoization branch April 28, 2026 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance scope: all components Widespread work has an impact on almost all components.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants