EasyPerformance📖 Theory Question

What is code splitting and lazy loading in React?

💡

Hint

React.lazy + Suspense — load component code only when needed, reduces initial bundle

Full Answer

import { lazy, Suspense } from 'react';

// ❌ Eager import — bundled in the main chunk even if rarely used
import HeavyChart from './HeavyChart';

// ✅ Lazy — loaded only when rendered for the first time
const HeavyChart = lazy(() => import('./HeavyChart'));
const AdminPanel = lazy(() => import('./AdminPanel'));

// Suspense shows fallback while the chunk loads
function Dashboard() {
  return (
    }>
      
    
  );
}

// Route-based code splitting — most impactful
const Home    = lazy(() => import('./pages/Home'));
const Profile = lazy(() => import('./pages/Profile'));
const Admin   = lazy(() => import('./pages/Admin'));

function App() {
  return (
    }>
      
        } />
        } />
        } />
      
    
  );
}
💡 Route-level splitting is the highest-impact optimization. Admin panels, dashboards, and settings pages are perfect candidates — users on the landing page never need that code.

More Performance Questions

EasyWhat are debounce and throttle? When do you use each?EasyWhat causes memory leaks in JavaScript and how do you detect them?EasyWhat is the difference between reflow and repaint, and how do you avoid layout thrashing?MediumWhat is lazy loading and code splitting?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint