Optimizes React app performance (bundle size, rendering, lazy loading)
Analyzes and optimizes React applications by implementing code splitting, lazy loading, and rendering optimizations. Uses bundle analysis tools to identify performance bottlenecks and applies memoization patterns to reduce unnecessary re-renders.
/plugin marketplace add IvanTorresEdge/molcajete.ai/plugin install react@Molcajete.aiOptimizes React application performance following performance-patterns, bundle-optimization, and vite-configuration skills.
MUST reference these skills for guidance:
performance-patterns skill:
bundle-optimization skill:
vite-configuration skill:
post-change-verification skill:
<pkg> run format to format code
c. Run <pkg> run lint to lint code (ZERO warnings required)
d. Run <pkg> run type-check for type verification (ZERO errors required)
e. Run <pkg> test for affected tests
f. Document any pre-existing issues not caused by this changeimport { lazy, Suspense } from 'react';
// Lazy load heavy components
const Dashboard = lazy(() => import('./pages/Dashboard'));
const Analytics = lazy(() => import('./pages/Analytics'));
const Settings = lazy(() => import('./pages/Settings'));
function App(): React.ReactElement {
return (
<Suspense fallback={<LoadingSpinner />}>
<Routes>
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/analytics" element={<Analytics />} />
<Route path="/settings" element={<Settings />} />
</Routes>
</Suspense>
);
}
// vite.config.ts
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
output: {
manualChunks: {
'vendor-react': ['react', 'react-dom'],
'vendor-ui': ['@radix-ui/react-dialog', '@radix-ui/react-dropdown-menu'],
'vendor-query': ['@tanstack/react-query'],
},
},
},
},
});
import { memo, useMemo, useCallback } from 'react';
interface ItemProps {
item: Item;
onSelect: (id: string) => void;
}
// Memoize component to prevent re-renders
const ListItem = memo(function ListItem({ item, onSelect }: ItemProps): React.ReactElement {
const handleClick = useCallback(() => {
onSelect(item.id);
}, [item.id, onSelect]);
return (
<li onClick={handleClick}>
{item.name}
</li>
);
});
function ItemList({ items, onSelect }: { items: Item[]; onSelect: (id: string) => void }): React.ReactElement {
// Memoize expensive computations
const sortedItems = useMemo(
() => items.slice().sort((a, b) => a.name.localeCompare(b.name)),
[items]
);
// Memoize callback to prevent child re-renders
const handleSelect = useCallback((id: string) => {
onSelect(id);
}, [onSelect]);
return (
<ul>
{sortedItems.map((item) => (
<ListItem key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
);
}
import { useVirtualizer } from '@tanstack/react-virtual';
import { useRef } from 'react';
function VirtualList({ items }: { items: Item[] }): React.ReactElement {
const parentRef = useRef<HTMLDivElement>(null);
const virtualizer = useVirtualizer({
count: items.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 50, // Estimated row height
overscan: 5,
});
return (
<div ref={parentRef} className="h-[400px] overflow-auto">
<div
style={{
height: `${virtualizer.getTotalSize()}px`,
width: '100%',
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map((virtualItem) => (
<div
key={virtualItem.key}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}}
>
{items[virtualItem.index]?.name}
</div>
))}
</div>
</div>
);
}
// Next.js Image component
import Image from 'next/image';
function OptimizedImage(): React.ReactElement {
return (
<Image
src="/hero.jpg"
alt="Hero image"
width={1200}
height={600}
priority // Load immediately for LCP
placeholder="blur"
blurDataURL="data:image/jpeg;base64,..."
/>
);
}
// Vite with lazy loading
function LazyImage({ src, alt }: { src: string; alt: string }): React.ReactElement {
return (
<img
src={src}
alt={alt}
loading="lazy"
decoding="async"
className="w-full h-auto"
/>
);
}
import { Link, useNavigate } from 'react-router-dom';
import { useEffect } from 'react';
// Prefetch on hover
function NavLink({ to, children }: { to: string; children: React.ReactNode }): React.ReactElement {
const prefetch = () => {
// Dynamic import to prefetch
if (to === '/dashboard') {
import('./pages/Dashboard');
}
};
return (
<Link to={to} onMouseEnter={prefetch}>
{children}
</Link>
);
}
// Prefetch after initial load
function usePrefetchRoutes(): void {
useEffect(() => {
const timer = setTimeout(() => {
import('./pages/Dashboard');
import('./pages/Settings');
}, 2000); // Prefetch after 2s
return () => clearTimeout(timer);
}, []);
}
# Vite bundle analysis
npx vite-bundle-visualizer
# Next.js bundle analysis
ANALYZE=true npm run build
# Generic bundle analysis
npx source-map-explorer dist/**/*.js
| Metric | Target | Tool |
|---|---|---|
| LCP | < 2.5s | Lighthouse |
| FID | < 100ms | Lighthouse |
| CLS | < 0.1 | Lighthouse |
| Bundle Size | < 200KB (gzipped) | Bundle analyzer |
| Time to Interactive | < 3.8s | Lighthouse |
You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.