Code Splitting
Only load features when they are actually needed.
When you build a React app for production, it creates three types of files:
Out of all three, JavaScript is the biggest reason your app slows down.
A large amount of JavaScript causes problems at many stages:
Instead of loading everything in one go:
This idea is called Code Splitting.
This is used for heavy components (like charts, editors, or maps) that take a lot of space.
import { lazy, Suspense } from "react";
const HeavyChart = lazy(() => import("./HeavyChart"));
function Dashboard() { return ( <Suspense fallback={<p>Loading chart...</p>}> <HeavyChart /> </Suspense> );}lazy() -> this loads the component only when it’s actually needed (this is called a dynamic import)Suspense -> this shows a fallback message (like “Loading…”) while the component is being loadedThis gives the best results for real, full-sized apps.
import { lazy, Suspense } from "react";import { Routes, Route } from "react-router-dom";
const Home = lazy(() => import("./pages/Home"));const Settings = lazy(() => import("./pages/Settings"));const Reports = lazy(() => import("./pages/Reports"));
export default function AppRoutes() { return ( <Suspense fallback={<p>Loading page...</p>}> <Routes> <Route path="/" element={<Home />} /> <Route path="/settings" element={<Settings />} /> <Route path="/reports" element={<Reports />} /> </Routes> </Suspense> );}Without splitting, you get one giant file:
main.js (very large file with everything)With splitting, you get several smaller files instead:
main.jshome.chunk.jsreports.chunk.jssettings.chunk.jsThe browser only loads the chunks it actually needs
Vite already splits your code for you automatically, but you can control how it groups things.
// vite.config.jsexport default { build: { rollupOptions: { output: { manualChunks: { reactVendor: ["react", "react-dom", "react-router-dom"], chartVendor: ["chart.js"], }, }, }, },};Code Splitting
Only load features when they are actually needed.
Tree Shaking
Only import the specific functions you need from a library, not the whole thing.
Image Optimization
Use formats like WebP or AVIF, and make images responsive (fit different screen sizes).
Memoization
Stop components from re-rendering when they don’t need to.
Virtualization
For long lists, only render the items the user can actually see on screen.
Caching
Store static files smartly so they don’t need to be downloaded again and again.
The tools you can use for this:
React.memouseMemouseCallbackYou can use these tools:
Even if the network connection is fast, remember this:
Performance = Network + CPU + Rendering