DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. JavaScript Modules: Dynamic Import and Code Splitting
XLinkedInReddit
MediumFrontend Engineering

JavaScript Modules: Dynamic Import and Code Splitting

D
DevPrep Team
February 10, 2026·2 min read·0
Table of Contents
  • Static vs Dynamic Import
  • React Lazy Loading
  • Preloading Strategies
  • Conditional Loading
  • Webpack Magic Comments
  • Measuring Impact

Code splitting is the single most impactful performance optimization for SPAs. Dynamic imports make it possible.

Static vs Dynamic Import

// Static import — always loaded, bundled together
import { heavyChart } from "./charts";

// Dynamic import — loaded on demand
const { heavyChart } = await import("./charts");
// Returns a Promise that resolves to the module

React Lazy Loading

import { lazy, Suspense } from "react";

// Component loaded only when rendered
const Dashboard = lazy(() => import("./pages/Dashboard"));
const Settings = lazy(() => import("./pages/Settings"));

function App() {
  return (
    <Suspense fallback={<Loading />}>
      <Routes>
        <Route path="/dashboard" element={<Dashboard />} />
        <Route path="/settings" element={<Settings />} />
      </Routes>
    </Suspense>
  );
}

Preloading Strategies

// Preload on hover (component will likely be needed)
function NavLink({ to, component, children }) {
  const preload = () => component.preload?.();
  return (
    <Link to={to} onMouseEnter={preload}>
      {children}
    </Link>
  );
}

// Preload after initial render
useEffect(() => {
  // Preload routes the user is likely to visit
  import("./pages/Dashboard");
  import("./pages/Profile");
}, []);

Conditional Loading

// Load polyfills only when needed
if (!window.IntersectionObserver) {
  await import("intersection-observer");
}

// Load based on user role
const AdminPanel = user.isAdmin
  ? lazy(() => import("./AdminPanel"))
  : () => null;

// Load based on feature flag
if (features.newEditor) {
  const { Editor } = await import("./NewEditor");
}

Webpack Magic Comments

// Name the chunk
const Comp = lazy(() => import(/* webpackChunkName: "dashboard" */ "./Dashboard"));

// Prefetch (low priority, loaded during idle time)
import(/* webpackPrefetch: true */ "./Settings");

// Preload (high priority, loaded immediately)
import(/* webpackPreload: true */ "./CriticalComponent");

Measuring Impact

  • Chrome DevTools → Network → filter by JS to see chunk sizes
  • Webpack Bundle Analyzer for visual bundle composition
  • Lighthouse → "Reduce unused JavaScript" audit
  • Target: initial bundle under 200KB gzipped

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Static vs Dynamic Import
  • React Lazy Loading
  • Preloading Strategies
  • Conditional Loading
  • Webpack Magic Comments
  • Measuring Impact

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.