Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
Frontend System Design Roadmap
  1. 1.HTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC)
  2. 2.TCP and TLS Handshakes: Connection Setup Costs
  3. 3.Browser Network Stack and Connection Pooling
  4. 4.GET vs POST and Idempotency in HTTP
  5. 5.The Critical Rendering Path (CRP)
  6. 6.DOM, CSSOM and the Render Tree
  7. 7.Layout, Paint and Compositing
  8. 8.Reflows and Repaints Explained
  9. 9.Event Loop: Call Stack, Task and Microtask Queues
  10. 10.Async JavaScript: Promises and async/await
  11. 11.Web Workers: Multi-threading in the Browser
  12. 12.Browser Storage: Cookies, Web Storage, IndexedDB
  13. 13.Client-Side Storage: Limits and Security Risks
  14. 14.Choosing the Right Client-Side Storage
  15. 15.HTTP Caching: Cache-Control, ETag, Last-Modified
  16. 16.Browser-Level Caching Mechanisms
  17. 17.Cache Invalidation for Frontend Apps
  18. 18.Structuring a Large Single-Page App
  19. 19.Component-Based Architecture Principles
  20. 20.Props, State and Component Lifecycle
  21. 21.Module Bundling Basics
  22. 22.Core Web Vitals: LCP, INP and CLS
  23. 23.FCP and Time to Interactive (TTI) Explained
  24. 24.Measuring Performance: Lighthouse and WebPageTest
  25. 25.Asset Compression and Minification Basics
  26. 26.What Is a CDN and How Does It Work?
  27. 27.CDN Benefits: Latency, Availability, Offload
  28. 28.CDN Cache Behavior and Invalidation
  29. 29.Global State vs Local Component State
  30. 30.State Patterns: Redux, Zustand and Context
  31. 31.Data Fetching: React Query and SWR
  32. 32.Immutable Data in Frontend State
  33. 33.SSR vs CSR: Rendering Trade-offs
  34. 34.Hydration and Time to First Byte (TTFB)
  35. 35.Static Site Generation (SSG) for Speed and SEO
  36. 36.Incremental Static Regeneration (ISR)
  37. 37.Micro-Frontends: Core Principles
  38. 38.Micro-Frontend Integration Strategies
  39. 39.Communication Between Micro-Frontends
  40. 40.Micro-Frontends: Pros and Cons for Large Teams
  41. 41.Real-Time: Polling, SSE and WebSockets
  42. 42.Real-Time Use Cases: Chat, Alerts, Live Data
  43. 43.Designing a Real-Time Data Flow
  44. 44.Scaling WebSocket Connections
  45. 45.Responsive Images: srcset, sizes and picture
  46. 46.Modern Image Formats: WebP, AVIF, JPEG XL
  47. 47.Lazy Loading Images and Videos
  48. 48.Video Streaming: HLS, DASH, Adaptive Bitrate
  49. 49.REST and GraphQL for Frontend Engineers
  50. 50.Batching, Throttling and Debouncing API Calls
  51. 51.Handling API Errors in the Frontend
  52. 52.Authentication Flows: OAuth and JWT
  53. 53.Global Error Boundaries in React
  54. 54.Error Reporting: Sentry and LogRocket
  55. 55.Graceful Degradation and Fallback UIs
  56. 56.Real User Monitoring (RUM)
  57. 57.CORS: Cross-Origin Resource Sharing
  58. 58.Preventing XSS (Cross-Site Scripting)
  59. 59.CSRF Protection for Frontend Apps
  60. 60.Content Security Policy (CSP)
  61. 61.Storing Auth Tokens: Cookies vs localStorage
  62. 62.PWA Manifest and Installability
  63. 63.Service Workers: Lifecycle and Caching Strategies
  64. 64.Offline Support and Background Sync
  65. 65.Push Notifications for Web Apps
  66. 66.Webpack vs Vite vs Parcel
  67. 67.Code Splitting, Tree Shaking, Asset Optimization
  68. 68.Module Resolution and Hot Module Replacement
  69. 69.Monorepo Tooling: Nx and Lerna
  70. 70.Atomic Design for Component Libraries
  71. 71.Theming and Customization in Design Systems
  72. 72.Accessibility in Component Libraries
  73. 73.Component Library Docs, Testing and Versioning
  74. 74.Publishing and Consuming a Component Library
  75. 75.Full-Text Search: Levenshtein and Fuzzy Matching
  76. 76.Indexing Large Datasets in the Browser
  77. 77.Search Performance: Debouncing and Web Workers
  78. 78.Integrating Backend Search Services
  79. 79.Choosing a Chart Library: D3, Chart.js, Recharts
  80. 80.Rendering Large Datasets: Canvas vs SVG
  81. 81.Interactive, Responsive Data Visualizations
  82. 82.Data Fetching and Aggregation for Dashboards
  83. 83.A/B Test Design and Rollout Strategies
  84. 84.Client-Side vs Server-Side A/B Testing
  85. 85.Feature Flags and Remote Configuration
  86. 86.Experiment Data Collection and Analytics
Frontend System Design Roadmap
67

Frontend System Design · Lesson 67 of 86 · 4 min

Code Splitting, Tree Shaking, Asset Optimization

This is how you explain that your app isn't just fast—it's efficient. Get this right to show you understand the entire delivery pipeline.

Split your code to shrink the initial payload

Your first goal is to send the user the absolute minimum amount of code required to render the initial view. Everything else can wait. This is code splitting.

The most common strategy is route-based splitting. A user on /dashboard doesn't need the code for /settings. You achieve this with dynamic import(), which modern bundlers recognize and use to create separate output files, or "chunks".

Frameworks provide helpers for this. In React, you use React.lazy and Suspense.

// src/App.tsx
import React, { Suspense } from 'react';
import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom';

// These components will be in separate JS chunks.
const HomePage = React.lazy(() => import('./pages/HomePage'));
const AboutPage = React.lazy(() => import('./pages/AboutPage'));

const App: React.FC = () => {
  return (
    <Router>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>
      <Suspense fallback={<div>Loading...</div>}>
        <Routes>
          <Route path="/" element={<HomePage />} />
          <Route path="/about" element={<AboutPage />} />
        </Routes>
      </Suspense>
    </Router>
  );
};

export default App;

When the user navigates to /about, the browser makes a network request for the AboutPage chunk. Suspense shows a fallback UI until the chunk is downloaded and ready. You can also split on components, not just routes. A heavy chart library or a rich text editor can be loaded only when the user clicks a button to show it.

The trap: Over-splitting. Creating hundreds of tiny chunks can be slower than a few medium-sized ones. Each new chunk requires another HTTP request. While HTTP/2 mitigates this with multiplexing, there's still overhead. Analyze your bundle with a tool like Webpack Bundle Analyzer and group related, smaller modules into a single chunk if needed.

Shake the tree to eliminate dead code

Tree shaking is dead code elimination. Your bundler statically analyzes your import and export statements and removes any exported code that is never imported by another module.

This works because ES Modules are static. The dependency graph can be determined at build time without running any code. This is a key advantage over CommonJS, where a dynamic require() call can depend on runtime values, making static analysis impossible.

The key detail: For tree shaking to work on your dependencies, the library author must signal that their code is "pure." This is done by setting "sideEffects": false in their package.json. This tells the bundler that importing a module from the package has no side effects (like modifying window or applying global CSS). If it's set incorrectly, the bundler might remove code that was actually necessary.

If a library doesn't set this flag, your bundler will play it safe and include all its code, even if you only import one small function.

Optimize assets, especially images

JavaScript is often not the biggest performance problem. Large, unoptimized images are a common cause of slow page loads and a poor Largest Contentful Paint (LCP) score.

Focus on these three areas for images:

  1. Format: Use modern formats like WebP or AVIF. They offer better compression than JPEG or PNG. Use the <picture> element to provide fallbacks for older browsers.
  2. Size: Serve appropriately sized images for the user's viewport. Use the srcset and sizes attributes to let the browser choose the best image from a set of options. Don't send a 4000px-wide desktop hero image to a mobile device.
  3. Loading: Defer loading of off-screen images. The easiest way is with the native loading="lazy" attribute on the <img> tag. The browser will handle the rest.

Fonts are another critical asset. Use the WOFF2 format, subset fonts to include only the characters you need, and use the CSS property font-display: swap to prevent text from being invisible while the font file loads.

How to answer "How would you optimize a slow SPA?"

This question is a test of your systematic approach. Don't just list techniques. Show a process.

  1. Analyze first. State that you'd begin by profiling. Use Lighthouse to get a baseline and Webpack Bundle Analyzer to see what's actually in your JavaScript bundles. Identify the largest modules and assets.
  2. Attack the initial bundle. Your first priority is reducing the JavaScript needed for the initial render. Implement route-based code splitting. This is usually the biggest win.
  3. Verify dead code elimination. Check that tree shaking is working correctly and that you're not importing entire libraries when you only need a single function.
  4. Optimize assets. After JS, turn to images and fonts. Explain how you'd implement responsive images and lazy loading.
  5. Mention the basics. Minification and server compression (Brotli/Gzip) are standard practice, but mentioning them shows you haven't forgotten the fundamentals.

This flow demonstrates a structured, data-driven approach to performance optimization, which is exactly what an interviewer wants to hear.

Try it

Take a small multi-page React or Vue project. Refactor it to use React.lazy or defineAsyncComponent for its routes. Open your browser's network tab, disable the cache, and watch the separate JavaScript chunks get loaded as you navigate between pages.

Next: We'll see how bundlers find your files and update them instantly in Module Resolution and Hot Module Replacement.

Lock it in — apply this lesson under interview conditions.

Try a free mock interviewPractice system design problems
Previous

Webpack vs Vite vs Parcel

Next

Module Resolution and Hot Module Replacement