JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicsnextjsPerformance Optimization
PrevNext
nextjs
advanced
11 min read

Performance Optimization

bundle-analysis
code-splitting
dynamic-import
nextjs
optimization
performance
streaming
suspense

Next.js provides automatic code splitting per route, next/dynamic for lazy-loading heavy components, streaming with Suspense for progressive page loading, Server Components for zero-client-JS rendering, and bundle analysis tools — together these techniques minimize JavaScript sent to the browser.

Key Points

1Server Components

The biggest optimization — Server Components send zero JavaScript to the browser. Heavy dependencies in server code never appear in the client bundle.

2Streaming with Suspense

HTML sent progressively as components resolve — Suspense boundaries show fallbacks while slow data loads. loading.tsx creates automatic per-route loading states.

3next/dynamic

Lazy-loads Client Components — code downloads only when needed. Use for modals, charts, and heavy components below the fold.

4Bundle Optimization

Automatic per-route code splitting, optimizePackageImports for barrel exports, @next/bundle-analyzer for visualization.

What You'll Learn

  • Explain how Server Components reduce client-side JavaScript bundles
  • Implement streaming with Suspense and loading.tsx for progressive page loading
  • Use next/dynamic to lazy-load heavy Client Components
  • Analyze and optimize JavaScript bundles with bundle analyzer

Deep Dive

Next.js includes many performance optimizations out of the box, plus tools for manual optimization when needed. The App Router's Server Component architecture provides the biggest performance win by default.

Automatic Code Splitting

Next.js automatically splits JavaScript bundles per route — navigating to /about only downloads the JavaScript for that page, not the entire app. This happens automatically with no configuration. Shared dependencies (React, utility libraries) are split into common chunks that load once.

Server Components = Zero Client JS

The biggest performance optimization in the App Router: Server Components don't send any JavaScript to the browser. A page with 50 Server Components and 3 Client Components only ships JavaScript for those 3 Client Components. Heavy dependencies used in Server Components (markdown parsers, syntax highlighters, data processing libraries) never appear in the client bundle.

Dynamic Imports (next/dynamic)

For Client Components, next/dynamic provides lazy loading — the component's code downloads only when it's needed:

TypeScript
import dynamic from 'next/dynamic';
 
const HeavyChart = dynamic(() => import('./Chart'), {
  loading: () => <p>Loading chart...</p>,
  ssr: false, // Skip server rendering (useful for browser-only libs)
});

Use next/dynamic for: modals, charts, rich text editors, map components, and anything below the fold or triggered by user interaction.

Streaming with Suspense

Streaming sends HTML progressively as Server Components resolve, instead of waiting for the entire page to render:

HTML
import { Suspense } from 'react';
 
async function Page() {
  return (
    <div>
      <Header />  {/* Renders immediately */}
      <Suspense fallback={<Skeleton />}>
        <SlowDataSection />  {/* Streams in when ready */}
      </Suspense>
    </div>
  );
}

The instant header + skeleton fallback pattern dramatically improves perceived performance — users see meaningful content immediately while slower data loads.

loading.tsx files create automatic Suspense boundaries for route segments:

JSX
// app/dashboard/loading.tsx
export default function Loading() {
  return <DashboardSkeleton />;
}

Partial Prerendering (PPR)

An experimental Next.js feature that combines static and dynamic rendering in one route. The static shell (layout, above-the-fold content) is served instantly from the CDN, while dynamic parts (user-specific data, real-time content) stream in via Suspense boundaries. This gives static-site speed with dynamic-site flexibility.

Bundle Analysis

@next/bundle-analyzer visualizes what's in your JavaScript bundles:

Bash
npm install @next/bundle-analyzer
// next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({ enabled: true });
module.exports = withBundleAnalyzer(nextConfig);

Look for: unexpectedly large dependencies, duplicate packages, and Client Components that should be Server Components.

optimizePackageImports

For barrel-export libraries (lodash, lucide-react, @mui/icons-material), configure optimizePackageImports in next.config.js to enable tree-shaking:

JavaScript
experimental: {
  optimizePackageImports: ['lucide-react', '@mui/icons-material'],
}

This transforms import { Search } from 'lucide-react' into a direct import from the specific file, avoiding loading the entire icon library.

Key Interview Distinction

Server Components are the biggest performance win — zero client JS for most components. Automatic code splitting per route. next/dynamic lazy-loads heavy Client Components. Streaming with Suspense sends HTML progressively. loading.tsx creates automatic loading states. Bundle analysis identifies optimization opportunities. optimizePackageImports tree-shakes barrel exports.

Fun Fact

The Next.js team measured that the average Next.js 13 app using Server Components shipped 30-50% less client-side JavaScript compared to the equivalent Pages Router app — without any manual optimization. The reduction comes entirely from components that no longer need to send their code to the browser.

Learn These First

Server & Client Components

intermediate

Rendering Strategies

advanced

Continue Learning

Image & Font Optimization

beginner

Caching & Revalidation

advanced

Practice What You Learned

What performance optimization techniques does Next.js provide?
senior
performance
Next.js provides automatic optimizations including code splitting per route, image optimization via next/image, font optimization with next/font, script loading control with next/script, Server Components to reduce JavaScript bundles, Suspense for streaming, automatic prefetching of links, and edge deployment capabilities.
Previous
Image & Font Optimization
Next
Rendering Strategies
PrevNext