performance
MUI performance optimization — tree-shaking, bundle size, rendering, SSR
What this skill does
# MUI Performance Optimization
## Tree-Shaking — Named Imports Only
Use named imports from `@mui/material`. Never import from barrel files or index — bundlers
cannot tree-shake those effectively.
```tsx
// BAD — imports the entire @mui/material bundle (~300 KB+ gzipped)
import { Button, TextField, Dialog } from '@mui/material';
// GOOD — each import is individually tree-shaken
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Dialog from '@mui/material/Dialog';
```
### Icons — always deep import
```tsx
// BAD — imports all 2100+ icons (~1 MB+)
import { Delete, Edit, Add } from '@mui/icons-material';
// GOOD — only the used icon is bundled
import DeleteIcon from '@mui/icons-material/Delete';
import EditIcon from '@mui/icons-material/Edit';
import AddIcon from '@mui/icons-material/Add';
```
### babel-plugin-import (alternative for barrel imports)
If you must use named imports from barrels, configure the plugin to transform them:
```json
// .babelrc
{
"plugins": [
["babel-plugin-import", {
"libraryName": "@mui/material",
"libraryDirectory": "",
"camel2DashComponentName": false
}]
]
}
```
## Bundle Analysis
```bash
# Install source-map-explorer
npm install --save-dev source-map-explorer
# Add to package.json
"scripts": {
"analyze": "source-map-explorer 'build/static/js/*.js'"
}
# For Next.js, use @next/bundle-analyzer
npm install --save-dev @next/bundle-analyzer
```
```js
// next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
});
module.exports = withBundleAnalyzer({});
// Run:
// ANALYZE=true npm run build
```
```bash
# Webpack bundle analyzer (CRA or custom webpack)
npm install --save-dev webpack-bundle-analyzer
# In webpack config:
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
plugins: [new BundleAnalyzerPlugin()]
```
## Emotion Caching
Without caching, Emotion regenerates style sheets on every SSR request. Use `createCache`
with a `CacheProvider` for significant SSR performance gains.
```tsx
// lib/createEmotionCache.ts
import createCache from '@emotion/cache';
export default function createEmotionCache() {
return createCache({ key: 'css', prepend: true });
}
```
```tsx
// _app.tsx (Next.js Pages Router)
import { CacheProvider, EmotionCache } from '@emotion/react';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import createEmotionCache from '../lib/createEmotionCache';
const clientSideEmotionCache = createEmotionCache();
interface MyAppProps extends AppProps {
emotionCache?: EmotionCache;
}
export default function MyApp({ Component, emotionCache = clientSideEmotionCache, pageProps }: MyAppProps) {
return (
<CacheProvider value={emotionCache}>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</CacheProvider>
);
}
```
```tsx
// _document.tsx — inject emotion styles before MUI styles
import Document, { Html, Head, Main, NextScript } from 'next/document';
import createEmotionServer from '@emotion/server/create-instance';
import createEmotionCache from '../lib/createEmotionCache';
export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>{(this.props as any).emotionStyleTags}</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
}
MyDocument.getInitialProps = async (ctx) => {
const cache = createEmotionCache();
const { extractCriticalToChunks } = createEmotionServer(cache);
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) => (props) => <App emotionCache={cache} {...props} />,
});
const initialProps = await Document.getInitialProps(ctx);
const emotionStyles = extractCriticalToChunks(initialProps.html);
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(' ')}`}
key={style.key}
dangerouslySetInnerHTML={{ __html: style.css }}
/>
));
return { ...initialProps, emotionStyleTags };
};
```
## Avoiding Re-renders
### Memoize sx objects
The `sx` prop creates a new object on every render, causing Emotion to recalculate styles.
```tsx
import { useMemo } from 'react';
import Box from '@mui/material/Box';
// BAD — new object reference every render triggers style recalculation
function MyComponent({ isActive }: { isActive: boolean }) {
return (
<Box
sx={{
p: 2,
borderRadius: 1,
backgroundColor: isActive ? 'primary.light' : 'grey.100',
}}
>
Content
</Box>
);
}
// GOOD — memoize the sx object when it depends on props/state
function MyComponent({ isActive }: { isActive: boolean }) {
const sx = useMemo(
() => ({
p: 2,
borderRadius: 1,
backgroundColor: isActive ? 'primary.light' : 'grey.100',
}),
[isActive]
);
return <Box sx={sx}>Content</Box>;
}
// BEST for static styles — define outside component (zero recalculation)
const styles = {
container: { p: 2, borderRadius: 1 },
active: { backgroundColor: 'primary.light' },
inactive: { backgroundColor: 'grey.100' },
} as const;
function MyComponent({ isActive }: { isActive: boolean }) {
return (
<Box sx={[styles.container, isActive ? styles.active : styles.inactive]}>
Content
</Box>
);
}
```
### Memoize components
```tsx
import React, { memo, useCallback } from 'react';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import IconButton from '@mui/material/IconButton';
import DeleteIcon from '@mui/icons-material/Delete';
interface ItemProps {
id: string;
label: string;
onDelete: (id: string) => void;
}
// memo prevents re-render when parent re-renders but props are unchanged
const ProductItem = memo(function ProductItem({ id, label, onDelete }: ItemProps) {
return (
<ListItem
secondaryAction={
<IconButton aria-label={`Delete ${label}`} onClick={() => onDelete(id)}>
<DeleteIcon />
</IconButton>
}
>
<ListItemText primary={label} />
</ListItem>
);
});
// In parent — stabilize callback with useCallback
function ProductList({ items }: { items: Item[] }) {
const handleDelete = useCallback((id: string) => {
setItems((prev) => prev.filter((item) => item.id !== id));
}, []); // no deps — setItems is stable
return (
<List>
{items.map((item) => (
<ProductItem
key={item.id}
id={item.id}
label={item.name}
onDelete={handleDelete}
/>
))}
</List>
);
}
```
### Avoid inline function handlers in render
```tsx
// BAD — new function reference on every render
<Button onClick={() => handleSave(item.id)}>Save</Button>
// GOOD — stable reference
const handleSave = useCallback(() => {
doSave(item.id);
}, [item.id]);
<Button onClick={handleSave}>Save</Button>
```
## Virtualization for Large Lists
Render only visible rows — critical for DataGrid-like scenarios with 1000+ rows.
```tsx
// Option 1: MUI X DataGrid (built-in virtualization)
import { DataGrid } from '@mui/x-data-grid/DataGrid';
<DataGrid
rows={largeDataset} // 10,000+ rows — only renders ~20 visible rows
columns={columns}
getRowId={(row) => row.id}
/>
// Option 2: react-window for custom lists
import { FixedSizeList } from 'react-window';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
function VirtualizedList({ items }: { items: string[] }) {
const Row = ({ index, style }: { index: number; style: React.CSSProperties }) => (
<ListItem style={style} key={index} component="div" disablePadding>
<ListItemText primary={items[index]} />
</ListIteRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.