Loading...
Loading...
React performance optimization specialist. Expert in DevTools Profiler, memoization, Core Web Vitals, bundle optimization, and virtualization. Use this skill for performance bottlenecks, slow renders, large bundles, or memory issues in React applications.
npx skill4agent add duck4nh/antigravity-kit react-performance# Detect React version and concurrent features
npm list react --depth=0 2>/dev/null | grep react@ || node -e "console.log(require('./package.json').dependencies?.react || 'Not found')" 2>/dev/null
# Check for performance tools
npm list web-vitals webpack-bundle-analyzer @next/bundle-analyzer --depth=0 2>/dev/null | grep -E "(web-vitals|bundle-analyzer)"
# Detect build tools and configuration
if [ -f "next.config.js" ] || [ -f "next.config.mjs" ]; then echo "Next.js detected - check Image optimization, bundle analyzer"
elif [ -f "vite.config.js" ] || [ -f "vite.config.ts" ]; then echo "Vite detected - check rollup bundle analysis"
elif [ -f "webpack.config.js" ]; then echo "Webpack detected - check splitChunks config"
elif grep -q "react-scripts" package.json 2>/dev/null; then echo "CRA detected - eject may be needed for optimization"
fi
# Check for React DevTools Profiler availability
echo "React DevTools Profiler: Install browser extension for comprehensive profiling"
# Memory and virtualization libraries
npm list react-window react-virtualized @tanstack/react-virtual --depth=0 2>/dev/null | grep -E "(window|virtualized|virtual)"# Enable React DevTools Profiler
echo "1. Install React DevTools browser extension"
echo "2. Navigate to Profiler tab in browser DevTools"
echo "3. Click record button and perform slow user interactions"
echo "4. Stop recording and analyze results"
# Key metrics to examine:
echo "- Commit duration: Time to apply changes to DOM"
echo "- Render duration: Time spent in render phase"
echo "- Component count: Number of components rendered"
echo "- Priority level: Synchronous vs concurrent rendering"10 unnecessary renders: Implement React.memo with custom comparison
100 components rendering: Consider virtualization or pagination
# Check for React.memo usage
grep -r "React.memo\|memo(" --include="*.jsx" --include="*.tsx" src/ | wc -l
echo "Components using React.memo: $(grep -r 'React.memo\|memo(' --include='*.jsx' --include='*.tsx' src/ | wc -l)"
# Find inline object/function props (performance killers)
grep -r "={{" --include="*.jsx" --include="*.tsx" src/ | head -5
grep -r "onClick={() =>" --include="*.jsx" --include="*.tsx" src/ | head -5
# Check for missing useCallback/useMemo
grep -r "useCallback\|useMemo" --include="*.jsx" --include="*.tsx" src/ | wc -l// ❌ Bad - Inline objects cause unnecessary re-renders
function BadParent({ items }) {
return (
<div>
{items.map(item =>
<ExpensiveChild
key={item.id}
style={{ margin: '10px' }} // New object every render
onClick={() => handleClick(item.id)} // New function every render
item={item}
/>
)}
</div>
);
}
// ✅ Good - Stable references prevent unnecessary re-renders
const childStyle = { margin: '10px' };
const OptimizedChild = React.memo(({ item, onClick, style }) => {
// Component implementation
});
function GoodParent({ items }) {
const handleItemClick = useCallback((itemId) => {
handleClick(itemId);
}, []);
return (
<div>
{items.map(item =>
<OptimizedChild
key={item.id}
style={childStyle}
onClick={() => handleItemClick(item.id)}
item={item}
/>
)}
</div>
);
}# Analyze bundle size
if command -v npx >/dev/null 2>&1; then
if [ -d "build/static/js" ]; then
echo "CRA detected - analyzing bundle..."
npx webpack-bundle-analyzer build/static/js/*.js --no-open --report bundle-report.html
elif [ -f "next.config.js" ]; then
echo "Next.js detected - use ANALYZE=true npm run build"
elif [ -f "vite.config.js" ]; then
echo "Vite detected - use npm run build -- --mode analyze"
fi
fi
# Check for heavy dependencies
npm ls --depth=0 | grep -E "(lodash[^-]|moment|jquery|bootstrap)" || echo "No obviously heavy deps found"
# Find dynamic imports (code splitting indicators)
grep -r "import(" --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" src/ | wc -l
echo "Dynamic imports found: $(grep -r 'import(' --include='*.js' --include='*.jsx' --include='*.ts' --include='*.tsx' src/ | wc -l)"
# Check for React.lazy usage
grep -r "React.lazy\|lazy(" --include="*.jsx" --include="*.tsx" src/ | wc -l// Route-based splitting
import { lazy, Suspense } from 'react';
const HomePage = lazy(() => import('./pages/HomePage'));
const DashboardPage = lazy(() => import('./pages/DashboardPage'));
const ReportsPage = lazy(() => import('./pages/ReportsPage'));
function App() {
return (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="/dashboard" element={<DashboardPage />} />
<Route path="/reports" element={<ReportsPage />} />
</Routes>
</Suspense>
</Router>
);
}
// Component-level splitting
function FeatureWithHeavyModal() {
const [showModal, setShowModal] = useState(false);
const HeavyModal = useMemo(() =>
lazy(() => import('./HeavyModal')), []
);
return (
<div>
<button onClick={() => setShowModal(true)}>Show Modal</button>
{showModal && (
<Suspense fallback={<div>Loading modal...</div>}>
<HeavyModal onClose={() => setShowModal(false)} />
</Suspense>
)}
</div>
);
}# Check for cleanup patterns in useEffect
grep -r -A 5 "useEffect" --include="*.jsx" --include="*.tsx" src/ | grep -B 3 -A 2 "return.*=>" | head -10
# Find event listeners that might not be cleaned
grep -r "addEventListener\|attachEvent" --include="*.jsx" --include="*.tsx" src/ | wc -l
grep -r "removeEventListener\|detachEvent" --include="*.jsx" --include="*.tsx" src/ | wc -l
# Check for timers
grep -r "setInterval\|setTimeout" --include="*.jsx" --include="*.tsx" src/ | wc -l
grep -r "clearInterval\|clearTimeout" --include="*.jsx" --include="*.tsx" src/ | wc -l
# Memory monitoring setup check
grep -r "performance.memory" --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" src/ | wc -l// Proper cleanup implementation
function ComponentWithCleanup() {
const [data, setData] = useState(null);
useEffect(() => {
// Event listeners
const handleScroll = () => {
console.log('Scrolled');
};
const handleResize = debounce(() => {
console.log('Resized');
}, 100);
// Timers
const interval = setInterval(() => {
fetchLatestData().then(setData);
}, 5000);
// Async operations with AbortController
const controller = new AbortController();
fetchInitialData(controller.signal)
.then(setData)
.catch(err => {
if (!err.name === 'AbortError') {
console.error('Fetch failed:', err);
}
});
// Add listeners
window.addEventListener('scroll', handleScroll);
window.addEventListener('resize', handleResize);
// Cleanup function
return () => {
clearInterval(interval);
controller.abort();
window.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', handleResize);
};
}, []);
return <div>Component content: {data?.title}</div>;
}
// Memory monitoring hook
function useMemoryMonitor(componentName) {
useEffect(() => {
if (!performance.memory) return;
const logMemory = () => {
console.log(`${componentName} memory:`, {
used: (performance.memory.usedJSHeapSize / 1048576).toFixed(2) + 'MB',
total: (performance.memory.totalJSHeapSize / 1048576).toFixed(2) + 'MB'
});
};
const interval = setInterval(logMemory, 10000);
return () => clearInterval(interval);
}, [componentName]);
}# Check for large data rendering patterns
grep -r -B 2 -A 2 "\.map(" --include="*.jsx" --include="*.tsx" src/ | grep -E "items\.|data\.|list\." | head -5
# Look for virtualization libraries
npm list react-window react-virtualized @tanstack/react-virtual --depth=0 2>/dev/null | grep -E "(window|virtualized|virtual)"
# Check for pagination patterns
grep -r "page\|limit\|offset\|pagination" --include="*.jsx" --include="*.tsx" src/ | head -3// react-window implementation
import { FixedSizeList as List } from 'react-window';
const VirtualizedList = ({ items }) => {
const Row = ({ index, style }) => (
<div style={style}>
<ItemComponent item={items[index]} />
</div>
);
return (
<List
height={600} // Viewport height
itemCount={items.length}
itemSize={80} // Each item height
overscanCount={5} // Items to render outside viewport
>
{Row}
</List>
);
};
// Variable size list for complex layouts
import { VariableSizeList } from 'react-window';
const DynamicList = ({ items }) => {
const getItemSize = useCallback((index) => {
// Calculate height based on content
return items[index].isExpanded ? 120 : 60;
}, [items]);
const Row = ({ index, style }) => (
<div style={style}>
<ComplexItemComponent item={items[index]} />
</div>
);
return (
<VariableSizeList
height={600}
itemCount={items.length}
itemSize={getItemSize}
overscanCount={3}
>
{Row}
</VariableSizeList>
);
};# Install web-vitals library
npm install web-vitals
# Check for existing monitoring
grep -r "web-vitals\|getCLS\|getFID\|getLCP" --include="*.js" --include="*.jsx" --include="*.ts" --include="*.tsx" src/ | wc -l// Comprehensive Core Web Vitals monitoring
import { getCLS, getFID, getFCP, getLCP, getTTFB } from 'web-vitals';
function setupWebVitalsMonitoring() {
const sendToAnalytics = (metric) => {
console.log(metric.name, metric.value, metric.rating);
// Send to your analytics service
gtag('event', metric.name, {
value: Math.round(metric.name === 'CLS' ? metric.value * 1000 : metric.value),
event_label: metric.id,
non_interaction: true,
});
};
getCLS(sendToAnalytics);
getFID(sendToAnalytics);
getFCP(sendToAnalytics);
getLCP(sendToAnalytics);
getTTFB(sendToAnalytics);
}
// LCP optimization component
function OptimizedHero({ imageUrl, title }) {
return (
<div>
<img
src={imageUrl}
alt={title}
// Optimize LCP
fetchpriority="high"
decoding="async"
// Prevent CLS
width={800}
height={400}
style={{ width: '100%', height: 'auto' }}
/>
<h1>{title}</h1>
</div>
);
}
// CLS prevention with skeleton screens
function ContentWithSkeleton({ isLoading, content }) {
if (isLoading) {
return (
<div style={{ height: '200px', backgroundColor: '#f0f0f0' }}>
<div className="skeleton-line" style={{ height: '20px', marginBottom: '10px' }} />
<div className="skeleton-line" style={{ height: '20px', marginBottom: '10px' }} />
<div className="skeleton-line" style={{ height: '20px', width: '60%' }} />
</div>
);
}
return <div style={{ minHeight: '200px' }}>{content}</div>;
}import { useTransition, useState, useMemo } from 'react';
function SearchResults() {
const [isPending, startTransition] = useTransition();
const [query, setQuery] = useState('');
const [results, setResults] = useState([]);
const handleSearch = (newQuery) => {
// Urgent update - immediate UI feedback
setQuery(newQuery);
// Non-urgent update - can be interrupted
startTransition(() => {
const filtered = expensiveSearchOperation(data, newQuery);
setResults(filtered);
});
};
return (
<div>
<input
value={query}
onChange={(e) => handleSearch(e.target.value)}
placeholder="Search..."
/>
{isPending && <div>Searching...</div>}
<ResultsList results={results} />
</div>
);
}
// useDeferredValue for expensive renders
function FilteredList({ filter, items }) {
const deferredFilter = useDeferredValue(filter);
const filteredItems = useMemo(() => {
// This expensive calculation uses deferred value
return items.filter(item =>
item.name.toLowerCase().includes(deferredFilter.toLowerCase())
);
}, [items, deferredFilter]);
const isStale = filter !== deferredFilter;
return (
<div style={{ opacity: isStale ? 0.5 : 1 }}>
{filteredItems.map(item =>
<Item key={item.id} {...item} />
)}
</div>
);
}// ❌ Bad - Single large context
const AppContext = createContext({
user: null,
theme: 'light',
notifications: [],
settings: {},
currentPage: 'home'
});
// ✅ Good - Separate contexts by concern
const UserContext = createContext(null);
const ThemeContext = createContext('light');
const NotificationContext = createContext([]);
// Context value memoization
function AppProvider({ children }) {
const [user, setUser] = useState(null);
const [theme, setTheme] = useState('light');
// Memoize context value to prevent unnecessary re-renders
const userContextValue = useMemo(() => ({
user,
setUser,
login: (credentials) => loginUser(credentials).then(setUser),
logout: () => logoutUser().then(() => setUser(null))
}), [user]);
const themeContextValue = useMemo(() => ({
theme,
setTheme,
toggleTheme: () => setTheme(prev => prev === 'light' ? 'dark' : 'light')
}), [theme]);
return (
<UserContext.Provider value={userContextValue}>
<ThemeContext.Provider value={themeContextValue}>
{children}
</ThemeContext.Provider>
</UserContext.Provider>
);
}
// Context selector pattern for fine-grained updates
function useUserContext(selector) {
const context = useContext(UserContext);
if (!context) {
throw new Error('useUserContext must be used within UserProvider');
}
return useMemo(() => selector(context), [context, selector]);
}
// Usage with selector
function UserProfile() {
const userName = useUserContext(ctx => ctx.user?.name);
const isLoggedIn = useUserContext(ctx => !!ctx.user);
return (
<div>
{isLoggedIn ? `Welcome ${userName}` : 'Please log in'}
</div>
);
}# Webpack Bundle Analyzer
npx webpack-bundle-analyzer build/static/js/*.js --no-open --report bundle-report.html
# Next.js Bundle Analysis
ANALYZE=true npm run build
# Vite Bundle Analysis
npm run build -- --mode analyze
# Manual bundle inspection
ls -lah build/static/js/ | sort -k5 -hr# Lighthouse performance audit
npx lighthouse http://localhost:3000 --only-categories=performance --view
# Chrome DevTools performance
echo "Use Chrome DevTools > Performance tab to record and analyze runtime performance"
# React DevTools profiler
echo "Use React DevTools browser extension > Profiler tab for React-specific insights"# Node.js memory debugging
node --inspect --max-old-space-size=4096 scripts/build.js
# Memory usage monitoring in browser
echo "Use performance.memory API and Chrome DevTools > Memory tab"# Performance regression testing
npm test -- --coverage --watchAll=false --testPathPattern=performance
# Bundle size tracking
npm run build && ls -lah build/static/js/*.js | awk '{sum += $5} END {print "Total bundle:", sum/1024/1024 "MB"}'
# Memory leak detection
echo "Run app for 30+ minutes with typical usage patterns, monitor memory in DevTools"// Runtime performance monitoring
function AppWithMonitoring() {
const onRender = (id, phase, actualDuration, baseDuration, startTime, commitTime) => {
// Alert on slow renders
if (actualDuration > 16) {
analytics.track('slow_render', {
componentId: id,
phase,
duration: actualDuration,
timestamp: commitTime
});
}
};
return (
<Profiler id="App" onRender={onRender}>
<App />
</Profiler>
);
}