Back
Web Fundamentals

Lazy Loading: Load Resources On-Demand

Web Fundamentals
Build & Deployment: Monorepo, CI/CD, Strategies & Release SafetyState Management: Choosing the Right SolutionRedux: Predictable State Container (RTK + RTK Query)React Query (TanStack Query): Server State CachingData Fetching Patterns: REST, GraphQL, tRPC & Real-timeGraphQL Fundamentals for Frontend: Shape, Caching, and TradeoffsgRPC-Web Fundamentals: Browser Constraints and Proxy ModelCaching Strategies: Client, Server & EdgeData Normalization: Organizing State for PerformanceAPI Design Best Practices: Pagination, Errors, Versioning & Type SafetyAPI Versioning Strategies for Frontend CompatibilityPagination: Offset vs Cursor-BasedRate Limiting & API Resilience: Retries, Backoff, Jitter, IdempotencyHow Frontend Developers Can Handle Millions of API Requests Without Crashing EverythingBrowser Storage: Cookies, SessionStorage, LocalStorage, IndexedDBReal-time Communication: WebSockets, SSE & PollingWebRTC: Real-Time Communication in the BrowserCore Web Vitals: LCP, INP & CLSPerformance Optimization Trade-offsCritical Resource Prioritization: Optimize Loading OrderCode Splitting: Optimize Bundle Size with Dynamic ImportsTree Shaking: Eliminate Dead Code from Your BundleLazy Loading: Load Resources On-DemandResource Hints: Preload, Prefetch & PreconnectText Compression: Gzip and BrotliImage & Video Optimization: Modern Formats & TechniquesAdaptive Loading: Optimize for Device & NetworkList Virtualization: Render Large Lists EfficientlyWeb Workers vs Main Thread: Offloading Heavy WorkMemory Leaks in Frontend Apps: Detection & PreventionManaging Third-Party Scripts: Optimization StrategiesHow CDNs Work: Edge Delivery, Caching & PerformanceHTTP Caching Deep Dive: Cache-Control, ETag & RevalidationService Workers & Offline Strategy: Cache First, Network First & Update LifecyclePWA Fundamentals: Manifest, Installability & Offline UXCritical Rendering PathScript Loading: async vs deferEvent Loop: Understanding JavaScript Execution ModelJavaScript Module Systems: CJS vs ESM vs UMDDynamic Module Loading: import() FunctionImport on Interaction: Load When User InteractsImport on Visibility: Lazy Loading with IntersectionObserverBrowser Rendering Pipeline & Layout ThrashingRendering Strategies: CSR vs SSR vs SSG vs ISRStreaming SSR: Progressive HTML StreamingIslands Architecture: Independent Component HydrationReact Server Components: Zero-JS Server RenderingFramework Reactivity: React, Vue, Svelte & SolidHTTP/1.1 vs HTTP/2 vs HTTP/3 (QUIC) for Frontend PerformanceDNS Resolution: Path, TTL, Caching & Frontend ImpactCross-Site Scripting (XSS) AttacksCross-Site Request Forgery (CSRF) AttacksCORS Explained: Cross-Origin Resource SharingCORS Preflight in Practice: Credentials, Simple Requests & MisconfigurationsContent Security Policy (CSP)Why is HTTPS Secure? Understanding TLS/SSLAuthorization Best PracticesCookie Security & Session Hardening: SameSite, HttpOnly, Secure
easyPerformance

Lazy Loading: Load Resources On-Demand

TL;DRLazy load everything below the fold. Use native loading='lazy', React.lazy() + Suspense, and Intersection Observer.
High Signal
Google
Meta
Netflix
Agoda
Meesho
30-Second Answerstart every interview with this

Lazy loading defers non-critical resources until they are needed. It improves initial load performance, reduces bandwidth usage, and boosts Core Web Vitals (especially LCP and CLS). Apply it to images, components, and third-party scripts.

Instead of unloading the entire truck (all resources) at the front door immediately, the driver only brings items when you actually need them — hero image first, then below-fold content, heavy components only when you open the door (interact/scroll).

Initial Load: Critical Resources Only
Scroll / Interaction Trigger
Lazy Load On-Demand
Smaller Initial Bundle + Faster LCP

1Native Image Lazy Loading

Use the loading='lazy' attribute. Modern browsers handle it automatically. Always set explicit width/height to prevent CLS.

image.htmlhtml
<img src="hero.jpg" loading="eager" width="1200" height="600" alt="Hero">
<img src="below-fold.jpg" loading="lazy" width="800" height="500" alt="Content">

2React Component Lazy Loading

Use React.lazy() + Suspense for heavy components like modals, charts, and editors.

3Intersection Observer & Custom Strategies

For advanced control, use Intersection Observer to load content when it approaches the viewport.

4Third-Party Scripts & Advanced Patterns

Load analytics, chat widgets, and maps after page interactive or on user interaction.

PropertyEager LoadingLazy Loading
ConsLarger initial bundle, Slower LCPPossible interaction delay, Needs fallbacks
ProsNo delays on use, SimplerFaster initial load, Smaller bundle
Best ForAbove-the-fold critical contentBelow-fold, conditional, heavy features

Eager Loading

Cons

Larger initial bundle, Slower LCP

Pros

No delays on use, Simpler

Best For

Above-the-fold critical content

Lazy Loading

Cons

Possible interaction delay, Needs fallbacks

Pros

Faster initial load, Smaller bundle

Best For

Below-fold, conditional, heavy features

Common questions

  • ›“How do you implement lazy loading in a React app?”
  • ›“What are the trade-offs of lazy loading?”
  • ›“How does native image lazy loading work?”
  • ›“When should you lazy load vs eager load?”

What interviewers look for

  • Practical use of React.lazy() + Suspense
  • Understanding of loading states and error handling
  • Knowledge of native vs custom lazy loading
  • Connection to Core Web Vitals (LCP/CLS)

Short answer (60 sec)

Use native loading='lazy' for images, React.lazy() + Suspense for components, and Intersection Observer for custom needs. Lazy load everything below the fold while keeping critical content eager.

Detailed answer (senior level)

Lazy loading defers non-critical resources until needed. For images, use loading='lazy' and always set dimensions. In React, use dynamic imports with Suspense and Error Boundaries. Load third-party scripts on interaction or after load. Combine with prefetching for better UX. Always measure impact on LCP and INP.

  • Lazy loading above-the-fold / LCP content
  • Forgetting loading states and error boundaries
  • Not setting image dimensions (causes CLS)
  • Lazy loading tiny components (overhead > benefit)
  • Ignoring older browser fallbacks
Key Takeaways
  • ✓Lazy load everything below the fold for better LCP
  • ✓Use native loading='lazy' for images
  • ✓React.lazy() + Suspense is the standard for components
  • ✓Always provide meaningful fallbacks and error handling
  • ✓Combine with prefetching on hover for smoother UX
  • ✓Measure before and after with real-user metrics
Previous TopicTree Shaking: Eliminate Dead Code from Your BundleNext Topic Resource Hints: Preload, Prefetch & Preconnect

On this page