EasyBundle Optimization📖 Theory Question

What is code splitting and how does dynamic import() enable it?

💡

Hint

Split the bundle into chunks loaded on demand — dynamic import() is the mechanism, React.lazy is the React wrapper

Full Answer

Code splitting divides the JavaScript bundle into smaller chunks that are loaded on demand rather than upfront, reducing initial load time.

How dynamic import() works:

// Without code splitting — everything in one bundle
import HeavyChart from './HeavyChart';

// With code splitting — HeavyChart loaded only when needed
const HeavyChart = React.lazy(() => import('./HeavyChart'));

function Dashboard() {
  return (
    <Suspense fallback={<Spinner />}>
      <HeavyChart />
    </Suspense>
  );
}

Bundler behaviour: when the bundler (Webpack/Vite/Rollup) sees a dynamic import(), it emits a separate chunk file. That chunk is only downloaded when the import() call executes at runtime.

Common split points:

  • Route-level — each page/route is a separate chunk (most impactful).
  • Heavy library — import('chart.js') only when a chart is rendered.
  • Feature flag — only load premium features for Pro users.

More Bundle Optimization Questions

EasyWhat is tree shaking and what conditions must be met for it to work?EasyHow do you analyze and diagnose large bundle sizes?EasyWhat is vendor chunk splitting and why is it important for caching?MediumWhat is the module/nomodule pattern for differential serving?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint