Performance: Code splitting and lazy loading
This concept affects page load speed by reducing initial bundle size and deferring non-critical code loading.
import { lazy, Suspense } from 'react'; const BigComponent = lazy(() => import('./BigComponent')); export default function Page() { return <Suspense fallback={<div>Loading...</div>}><BigComponent /></Suspense>; }
import { BigComponent } from './BigComponent'; export default function Page() { return <BigComponent />; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single large bundle | Minimal | Single large reflow | High paint cost due to blocking | [X] Bad |
| Code splitting with lazy loading | Minimal | Multiple small reflows on demand | Lower paint cost, faster initial render | [OK] Good |
import React, { Suspense } from 'react';
const LazyComp = React.lazy(() => import('./LazyComp'));
export default function App() {
return (
Loading...}>
);
}App renders?const LazyComp = React.lazy(import('./LazyComp'));
export default function App() {
return ;
}Header and Footer. You want to show a spinner while either loads. Which is the best way to implement this?Pass the test and this pattern moves unknown