Skip to content

[combobox] Stop filtering grouped items after limit - #5086

Merged
atomiks merged 4 commits into
mui:masterfrom
lyzno1:codex/combobox-limit-filtering
Jun 23, 2026
Merged

atomiks merged 4 commits into
mui:masterfrom
lyzno1:codex/combobox-limit-filtering

Conversation

@lyzno1

@lyzno1 lyzno1 commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • stop filtering grouped combobox items once the configured limit has been reached
  • add a regression test that verifies items after the limit are not passed through the custom filter

Root cause

Grouped combobox filtering built a full filtered array for each group before slicing it to the remaining limit. This performed unnecessary filter work when the limit had already been satisfied inside the current group.

Validation

  • pnpm test:jsdom Combobox --no-watch
  • pnpm eslint -- packages/react/src/combobox/root/AriaCombobox.tsx packages/react/src/combobox/root/ComboboxRoot.test.tsx
  • pnpm typescript
  • pnpm exec prettier --check packages/react/src/combobox/root/AriaCombobox.tsx packages/react/src/combobox/root/ComboboxRoot.test.tsx

@lyzno1 lyzno1 changed the title fix(combobox): stop filtering grouped items after limit [combobox] Stop filtering grouped items after limit Jun 19, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 19, 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@5086
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5086
    

commit: 10df530

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+16B(0.00%) ▼-1B(0.00%)

Details of bundle changes

Performance

Total duration: 1,325.80 ms +31.83 ms(+2.5%) | Renders: 78 (+0)

Test Duration Renders
Select mount (200 instances) 127.21 ms ▼-43.75 ms(-25.6%) 3 (+0)
Combobox type — 500 items, narrows to ~11 (type "Row 25") 27.47 ms ▼-9.75 ms(-26.2%) 17 (+0)
Checkbox mount (500 instances) 98.20 ms +15.94 ms(+19.4%) 1 (+0)

11 tests within noise — details

Metric alarms

Test Metric Change
Checkbox mount (500 instances) bench:paint 🔺 +25.00 ms

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

@netlify

netlify Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 10df530
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a3a16793216200008a622e9
😎 Deploy Preview https://deploy-preview-5086--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@lyzno1
lyzno1 marked this pull request as ready for review June 19, 2026 12:37

@flaviendelangle flaviendelangle 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.

PR review

This is a self-contained performance optimization to grouped combobox filtering: instead of filtering an entire group and then slicing to the remaining limit, it filters item-by-item and stops once the per-group remaining limit is reached. I verified the output is behaviorally identical to the old code across all branches (filterQuery === '', limit === -1/Infinity, partial-limit groups, empty groups), so the only observable change is fewer filter calls — which is the intent and matches the already-existing non-grouped path. Nothing is merge-blocking; only two minor optional notes.

Bugs (0)

No findings.

The remainingLimit is always ≥ 1 when the loop body runs (the top-of-loop guard currentCount >= limit breaks first), so the slice(0, remainingLimit) and length >= remainingLimit checks never degenerate to a zero/empty window. The removed if (candidateItems.length === 0) continue; is correctly subsumed by the existing if (itemsToTake.length > 0) push guard, so empty groups still don't advance currentCount or get pushed. Output and ordering match the previous implementation exactly.

Tests (1)

1. ℹ️ Redundant .not.toBe(null) on getByRole

Location: packages/react/src/combobox/root/ComboboxRoot.test.tsx:5189

expect(screen.getByRole('option', { name: 'apple' })).not.toBe(null);
expect(screen.getByRole('option', { name: 'apricot' })).not.toBe(null);

getByRole throws when the element is absent, so the .not.toBe(null) assertion can never observe a null — the presence check is already enforced by the call itself. The adjacent absence assertions correctly use queryByRole(...).toBe(null). This is cosmetic; the test still pins the intended behavior (including the strong assertion on filter.mock.calls that avocado and artichoke are never filtered, which is what makes this a real regression test for the optimization).

Failure scenario: No functional gap — purely a slightly misleading assertion. Dropping .not.toBe(null) (or switching to findByRole) would read more idiomatically.

Fix: Use screen.getByRole('option', { name: 'apple' }); as the presence assertion, or expect(screen.queryByRole(...)).not.toBe(null).

Simplifications (1)

1. ℹ️ filterQuery emptiness is checked twice

Location: packages/react/src/combobox/root/AriaCombobox.tsx:281

const itemsToTake = filterQuery === '' ? group.items.slice(0, remainingLimit) : [];

if (filterQuery !== '') {
  for (const item of group.items) {
    ...
  }
}

The ternary and the following if test the same filterQuery condition in opposite directions. An if/else would express the two mutually exclusive paths once and avoid the empty-array placeholder. This is a readability nit, not a correctness issue.

Failure scenario: Slightly harder to read — a reader must connect the : [] placeholder to the populating loop below it.

Fix (optional):

const remainingLimit = limit > -1 ? limit - currentCount : Infinity;
let itemsToTake: Value[];
if (filterQuery === '') {
  itemsToTake = group.items.slice(0, remainingLimit);
} else {
  itemsToTake = [];
  for (const item of group.items) {
    if (itemsToTake.length >= remainingLimit) {
      break;
    }
    if (filter(item, filterQuery, itemToStringLabel)) {
      itemsToTake.push(item);
    }
  }
}

Docs (0)

No findings.

Verdict

Approve — the optimization is behavior-preserving across every branch, the regression test correctly asserts on filter-call count, and the only remaining items are two optional style notes.


🤖 Review generated with Claude Code

@flaviendelangle flaviendelangle added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. performance component: combobox Changes related to the combobox component. labels Jun 19, 2026
@atomiks
atomiks merged commit f8d9cbc into mui:master Jun 23, 2026
6 checks passed
@lyzno1
lyzno1 deleted the codex/combobox-limit-filtering branch June 23, 2026 06:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: combobox Changes related to the combobox component. performance 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.

3 participants