Skip to content

[utils] Improve mergeSlotProps performance - #48002

Merged
ZeeshanTamboli merged 9 commits into
mui:masterfrom
mj12albert:perf/merge-slot-props
Sep 3, 2026
Merged

ZeeshanTamboli merged 9 commits into
mui:masterfrom
mj12albert:perf/merge-slot-props

Conversation

@mj12albert

@mj12albert mj12albert commented Mar 17, 2026 •

Copy link
Copy Markdown
Member

Borrowed some ideas from Base UI:

  • Detect event handlers with charCodeAt checks instead of regexes
  • Replace Object.keys().filter().forEach() with for loops to remove extra array allocations
  • For the common case where the style prop is not supplied (using sx instead of style) remove unnecessary spreads and Object.keys in mergeSlotProps
  • Removed unused omitEventHandlers in Material UI (unused duplicate), a canonical version is in utils

The +0.05% gzip size increase is probably mostly from replacing the regex with charCodeAt but worth it as it could be 20-30% faster on slower devices: mui/base-ui#456

@mj12albert mj12albert added the package: utils Specific to the utils package. label Mar 17, 2026
@mui-bot

mui-bot commented Mar 17, 2026 •

Copy link
Copy Markdown

Netlify deploy preview

https://deploy-preview-48002--material-ui.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/material 🔺+190B(+0.04%) 🔺+63B(+0.04%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 🔺+183B(+1.19%) 🔺+74B(+1.23%)

Details of bundle changes

Generated by 🚫 dangerJS against d72a7f3

@mj12albert mj12albert added internal Behind-the-scenes enhancement. Formerly called “core”. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Mar 17, 2026
@mj12albert
mj12albert force-pushed the perf/merge-slot-props branch from c643f65 to d72a7f3 Compare March 17, 2026 11:50
@mj12albert
mj12albert marked this pull request as ready for review March 17, 2026 13:16

@codeCraft-Ritik codeCraft-Ritik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great contribution! The implementation is clean and integrates well with the existing codebase

@silviuaavram silviuaavram left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cool stuff!

@silviuaavram

Copy link
Copy Markdown
Member

Make sure to mark it as cherry pick if it's for v7

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label May 6, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 3, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-48002--material-ui.netlify.app/
QR code for https://deploy-preview-48002--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+94B(+0.02%) 🔺+10B(+0.01%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 🔺+93B(+0.62%) ▼-13B(-0.22%)

Details of bundle changes


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

@ZeeshanTamboli ZeeshanTamboli changed the title [internal] mergeSlotProps improvements [utils] mergeSlotProps improvements Sep 3, 2026
@ZeeshanTamboli ZeeshanTamboli added the package: material-ui Specific to Material UI. label Sep 3, 2026
@ZeeshanTamboli ZeeshanTamboli changed the title [utils] mergeSlotProps improvements [utils] Reduce allocations in mergeSlotProps Sep 3, 2026
@ZeeshanTamboli ZeeshanTamboli changed the title [utils] Reduce allocations in mergeSlotProps [utils] Improve mergeSlotProps performance Sep 3, 2026
@ZeeshanTamboli

Copy link
Copy Markdown
Member

@claude review

@github-actions

github-actions Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

PR review

Nothing here is merge-blocking. I traced every rewritten branch against its base version and the three refactors (mergeSlotProps in both packages, extractEventHandlers, omitEventHandlers) are behavior-preserving for all realistic inputs — the charCodeAt isEventHandler predicate is exactly equivalent to the old /^on[A-Z]/ + typeof === 'function' regex, and the deleted packages/mui-material/src/utils/omitEventHandlers.ts has no remaining references anywhere in the repo and was never re-exported from @mui/material/utils. The remaining findings are a leftover redundant check in the newly guarded style path, plus two gaps in the tests that are supposed to protect this refactor.

Tests (2)

1. 🟠 The new empty-style test passes even if the whole style-merge block is deleted

Location: packages/mui-utils/src/mergeSlotProps/mergeSlotProps.test.ts:168

it('preserves an empty style prop when all provided style props are empty objects', () => {
  const merged = mergeSlotProps({
    additionalProps: { style: {} },
    externalForwardedProps: { style: {} },
    externalSlotProps: { style: {} },
  });

  expect(merged.props).to.haveOwnProperty('style');
  expect(merged.props.style).to.deep.equal({});

In this scenario the style-merge block never assigns props.style — the outer guard passes, but Object.keys(mergedStyle).length > 0 is false, so the block is a no-op. The style key the test observes comes from the plain object spread (...additionalProps, ...externalForwardedProps, ...externalSlotProps at mergeSlotProps.ts:99-108), which this PR did not touch. The assertion therefore pins spread semantics, not the branch it is named after.

Failure scenario: Delete the entire if (…?.style || …) { … } block from the !getSlotProps branch and this test still passes green, so it provides no protection for the refactor it was added to guard.

Fix: Assert what the block actually does in this case — that merged.props.style is the same reference as the last provided style object (expect(merged.props.style).to.equal(externalSlotPropsStyle)), which fails if the block starts assigning a fresh merged object. Or rename the test to reflect that it is checking the spread fallthrough.

2. 🟠 No test merges styles through the !getSlotProps branch

Location: packages/mui-utils/src/mergeSlotProps/mergeSlotProps.ts:114

if (additionalProps?.style || externalForwardedProps?.style || externalSlotProps?.style) {
  const mergedStyle = {
    ...additionalProps?.style,
    ...externalForwardedProps?.style,
    ...externalSlotProps?.style,
  };

merges the style props (mergeSlotProps.test.ts:104) passes getSlotProps, so it only exercises the second branch. Of the two new tests, does not add a style prop… (line 147) also passes getSlotProps, and preserves an empty style prop… (line 168) hits this branch only with empty objects. That leaves the newly added guard on line 114 with no test where styles are non-empty and actually merged.

Failure scenario: Change line 114 to a wrong guard (e.g. drop the externalSlotProps?.style term). slotProps.x.style would then be silently dropped whenever no other source supplies a style — and the whole suite still passes.

Fix: Add a sibling of merges the style props without getSlotProps, asserting the additionalProps → externalForwardedProps → externalSlotProps precedence.

Simplifications (2)

1. 🟡 Object.keys(mergedStyle).length > 0 is now redundant inside the new guard

Location: packages/mui-utils/src/mergeSlotProps/mergeSlotProps.ts:120

if (additionalProps?.style || externalForwardedProps?.style || externalSlotProps?.style) {
  const mergedStyle = { ... };
  if (Object.keys(mergedStyle).length > 0) {
    props.style = mergedStyle;
  }
}

The outer guard already establishes that at least one source supplies a style, and every source that can supply one is also spread into props — so whenever Object.keys(mergedStyle).length === 0, props.style is already an empty object from the spread. Assigning mergedStyle unconditionally is deep-equal in every realistic case and drops one Object.keys array allocation per call on the path where styles do exist. It also avoids the current aliasing, where props.style can be a live reference to the caller's own style object. Same applies to the second branch at line 180.

Failure scenario: Every render of a slot that does set a style still allocates a throwaway Object.keys array purely to re-check a condition the outer guard has already answered — in a util whose stated purpose in this PR is removing exactly those allocations.

Fix: In both branches, keep the new outer guard and assign props.style = { ...a?.style, ...b?.style, ...c?.style } directly. The two new tests keep passing (deep.equal({}) holds either way); only finding 1's suggested identity assertion would need to match.

2. ℹ️ extractHandlers in the Material UI copy keeps the Object.keys().forEach() shape

Location: packages/mui-material/src/utils/mergeSlotProps.ts:20

Object.keys(defaultSlotPropsValue).forEach((key) => {

This is the one remaining forEach closure in the exact function the PR is optimizing, while the two mui-utils helpers were converted to indexed for loops. Converting it too would make the change consistent and remove the per-call callback allocation; it also runs once per mergeSlotProps call in the function-slot-props path, which is the hotter of the two.

Fix: Optional — mirror the indexed-loop form used in extractEventHandlers.ts:17-23.

Docs (1)

1. ℹ️ omitEventHandlers JSDoc understates the predicate

Location: packages/mui-utils/src/omitEventHandlers/omitEventHandlers.ts:6

 * A field is considered an event handler if it is a function with a name beginning with `on`.

The predicate (both before and after this PR) additionally requires an uppercase third character — the file's own test is named "should remove functions with names beginning with on followed by uppercase letter", and on, once, on2 are deliberately kept. Since the PR is touching this function and centralizing the rule in isEventHandler, it is a cheap moment to make the comment say what the code does. The same wording appears in extractEventHandlers.ts:6.

Fix: "…if it is a function whose name begins with on followed by an uppercase letter."

Verdict

Approve after nits — no behavior regressions found; the style-path tests added with this refactor do not actually pin the branches they name, and one redundant Object.keys check survives inside the new guard.


🤖 Review generated with Claude Code · Opus 5 (High) · medium review depth · 24 turns · 5m3s · $1.32 · run

@ZeeshanTamboli ZeeshanTamboli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Made some more improvements. Looks good to be merged now.

@ZeeshanTamboli
ZeeshanTamboli enabled auto-merge (squash) September 3, 2026 10:47
@ZeeshanTamboli
ZeeshanTamboli merged commit 0df62ec into mui:master Sep 3, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Behind-the-scenes enhancement. Formerly called “core”. package: material-ui Specific to Material UI. package: utils Specific to the utils package. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants