Skip to content

[system] Refactor defaultSxConfig for better gzip compression#47875

Closed
sai6855 wants to merge 13 commits intomui:masterfrom
sai6855:f-default-sx-config
Closed

[system] Refactor defaultSxConfig for better gzip compression#47875
sai6855 wants to merge 13 commits intomui:masterfrom
sai6855:f-default-sx-config

Conversation

@sai6855
Copy link
Copy Markdown
Member

@sai6855 sai6855 commented Feb 27, 2026

This PR refactors the defaultSxConfig object to use programmatic generation (via Object.fromEntries and array mapping) for many style property configurations, replacing repetitive manual property definitions.

Since defaultSxConfig runs only when createTheme is executed, and an app typically doesn’t have many createTheme calls, I think this PR change won’t affect performance much.

I have gotten good results by converting static object definitions to programmatic generation, in data-grid a PR reduced package size by 630 Bytes

mui/mui-x#21211

image

@mui-bot
Copy link
Copy Markdown

mui-bot commented Feb 27, 2026

Netlify deploy preview

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

Bundle size report

Bundle Parsed size Gzip size
@mui/material ▼-618B(-0.12%) ▼-40B(-0.03%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system ▼-633B(-0.92%) 🔺+14B(+0.06%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 80abb5f

@sai6855 sai6855 added performance scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI labels Feb 27, 2026
@sai6855 sai6855 force-pushed the f-default-sx-config branch 3 times, most recently from f1244af to 8c5b2f6 Compare March 1, 2026 16:43
@sai6855 sai6855 force-pushed the f-default-sx-config branch from 75f509c to 849282b Compare March 1, 2026 17:04
@sai6855 sai6855 marked this pull request as ready for review March 1, 2026 17:10
Copilot AI review requested due to automatic review settings March 1, 2026 17:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors packages/mui-system’s defaultSxConfig to reduce repetitive object literals by generating many entries programmatically, aiming to improve gzip compression of the shipped code.

Changes:

  • Generates border-related config entries via Object.fromEntries([...].map(...)).
  • Generates spacing (padding/margin) config entries programmatically.
  • Consolidates many “empty config” props (display/flexbox/grid/positions/typography) and sizing transform props into generated blocks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +10 to +18
...Object.fromEntries(
['border', 'borderTop', 'borderRight', 'borderBottom', 'borderLeft', 'outline'].map((prop) => [
prop,
{
themeKey: 'borders',
transform: borderTransform,
},
]),
),
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

This refactor introduces runtime work at module evaluation time (allocating arrays, running .map(), and calling Object.fromEntries()). In the PR description it’s stated that defaultSxConfig only runs when createTheme is executed, but defaultSxConfig is constructed when this module is imported. Consider updating the PR description (and/or adding a brief comment) to reflect when this code runs and why the added initialization work is acceptable.

Copilot uses AI. Check for mistakes.
gridTemplateAreas: {},
gridArea: {},

// positions
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

After moving gridColumn/gridRow/etc and position/top/right/bottom/left into the big Object.fromEntries(...) block above, the later // grid and // positions sections now only contain gap/rowGap/columnGap and zIndex respectively. Consider regrouping these entries (e.g., separate fromEntries blocks per section or moving zIndex into the same positions block) so the section headers still match the properties they contain and the config stays easy to scan.

Suggested change
// positions
// zIndex

Copilot uses AI. Check for mistakes.
@sai6855 sai6855 requested a review from ZeeshanTamboli March 17, 2026 06:27
@ZeeshanTamboli
Copy link
Copy Markdown
Member

I don't have any strong opinons on this. I'll let others give a decision on this to decide if it's worth the bundle size compression.

Copy link
Copy Markdown
Member

@mj12albert mj12albert left a comment

Choose a reason for hiding this comment

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

Don't think this works, I made Claude Code compress and measure this PR vs main:

Metric Original PR Delta
Raw source 4,742 B 3,962 B -780 B
Gzip (level 9) 976 B 1,056 B +80 B
Rough-minified + Gzip 871 B 905 B +34 B
Brotli (quality 11) 818 B 894 B +76 B

The gzip size actually INCREASED from 975 to 1058 bytes. That's +83 bytes gzipped. The raw size decreased from 4742 to 3962 (-780 bytes), but gzip is worse.

The original file has highly repetitive structure ({ style: padding } repeated 21 times, {} repeated many times, etc.) which is exactly the kind of redundancy gzip/brotli exploit. The Object.fromEntries/.map() pattern introduces unique syntax tokens (Object.fromEntries, .map, ... operators) that compress poorly relative to the repetitive literals they replace.

@siriwatknp
Copy link
Copy Markdown
Member

Prove me wrong but I don't think fromEntries is faster than object creation.

@sai6855
Copy link
Copy Markdown
Member Author

sai6855 commented Apr 5, 2026

Prove me wrong but I don't think fromEntries is faster than object creation.

You are right, Object.fromEntries is worse off interms of perf. Closing this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance scope: system The system, the design tokens / styling foundations used across components. eg. @mui/system with MUI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants