0
0
Reactframework~8 mins

Defining routes in React - Performance & Optimization

Choose your learning style9 modes available
Performance: Defining routes
MEDIUM IMPACT
This affects the initial page load speed and interaction responsiveness by controlling which components render based on the URL.
Setting up routes in a React app
React
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import { lazy, Suspense } from 'react';

const Home = lazy(() => import('./Home'));
const About = lazy(() => import('./About'));
const Contact = lazy(() => import('./Contact'));

function App() {
  return (
    <BrowserRouter>
      <Suspense fallback={<div>Loading...</div>}>
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
          <Route path="/contact" element={<Contact />} />
        </Routes>
      </Suspense>
    </BrowserRouter>
  );
}
Uses React.lazy and Suspense to load route components only when needed, reducing initial bundle size and speeding up LCP.
📈 Performance GainSaves initial bundle size by lazy loading routes, improving LCP and reducing blocking time.
Setting up routes in a React app
React
import { BrowserRouter, Route, Switch } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Switch>
        <Route path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
      </Switch>
    </BrowserRouter>
  );
}
Using Switch and component props causes all route components to be bundled and potentially loaded even if not needed immediately.
📉 Performance CostIncreases bundle size and delays LCP due to loading all route components upfront.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Eager route loading with Switch and component propsMore nodes due to all components loadedMultiple reflows if components mount unnecessarilyHigher paint cost due to larger initial render[X] Bad
Lazy loaded routes with React.lazy and SuspenseOnly necessary nodes renderedSingle reflow for active routeLower paint cost with smaller initial render[OK] Good
Rendering Pipeline
Route definitions determine which components the browser renders based on the URL. Efficient routing delays loading non-visible components, reducing style calculations and layout work.
JavaScript Parsing
Style Calculation
Layout
Paint
⚠️ BottleneckJavaScript Parsing and Execution due to large bundles with all routes loaded upfront
Core Web Vital Affected
LCP
This affects the initial page load speed and interaction responsiveness by controlling which components render based on the URL.
Optimization Tips
1Use React.lazy and Suspense to load route components only when needed.
2Replace Switch with Routes for modern React Router performance benefits.
3Avoid importing all route components upfront to keep initial bundle size small.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of lazy loading route components in React?
AReduces initial JavaScript bundle size and speeds up page load
BImproves CSS selector matching speed
CPrevents layout shifts caused by images
DIncreases the number of DOM nodes rendered
DevTools: Performance
How to check: Record a page load and look at the Main thread activity to see JavaScript parsing and scripting time. Check the Flame Chart for large blocking tasks.
What to look for: Look for long scripting tasks caused by large bundles and delayed LCP timings indicating slow route loading.