Static vs Dynamic Imports
You'll learn how static and dynamic imports differ and when each approach optimizes your application's performance.
What Are Static and Dynamic Imports?
Static imports load modules at the very beginning of your application, before any code runs. They use the traditional import statement at the top of your files. Every statically imported module becomes part of your initial JavaScript bundle.
Dynamic imports load modules on-demand using the import() function syntax. Think of it like the difference between packing your entire wardrobe for a trip (static) versus buying clothes only when you need them at your destination (dynamic).
Static Imports
// Loaded immediately, part of initial bundle
import { heavyComponent } from './heavy-component.js';
Static imports are synchronous and always execute before your code runs. They're predictable and easy to analyze, but every import increases your initial bundle size.
Dynamic Imports
// Loaded only when this code executes
const module = await import('./heavy-component.js');
Dynamic imports return a Promise and load code only when requested. This is the foundation of code splitting—breaking your application into smaller chunks that load on-demand.
When to Use Each
Use static imports for:
- Core functionality needed immediately
- Small, essential dependencies
- Code used on every page load
Use dynamic imports for:
- Large libraries used conditionally
- Route-based code splitting
- Features behind user interactions (modals, tabs)
- Admin panels or rarely-accessed sections
Dynamic imports reduce your initial bundle size, improving LCP and FID by loading less JavaScript upfront.
Key Takeaway: Static imports load everything immediately into your main bundle; dynamic imports enable on-demand loading to split code and improve initial performance metrics.