Skip to main content
Beta: Front-End Checklist is currently in beta. Some issues are still being fixed. Thanks for your patience.

Optimize JavaScript bundle size

Checks for JavaScript files that exceed recommended size limits to ensure fast interaction

Utilities
Quick take
Typical fix time 10 min
  • Keep individual JS bundles under 200KB (compressed)
  • Large bundles delay Time to Interactive (TTI) and First Input Delay (FID)
  • Use code-splitting and tree-shaking to reduce unused code
Why it matters: Excessive JavaScript increases parse and execution time, especially on low-end devices, leading to sluggish user experiences and high data costs.

Rule Details

Excessive JavaScript is one of the biggest contributors to poor web performance. Unlike images, which only need to be decoded, JavaScript must be downloaded, parsed, compiled, and executed.

Code Examples

Using Dynamic Imports (Next.js/React)

import dynamic from 'next/dynamic'
 
const HeavyComponent = dynamic(() => import('./HeavyComponent'), {
  loading: () => <p>Loading...</p>,
})
 
export default function Page() {
  return (
    <div>
      <HeavyComponent />
    </div>
  )
}

Tree Shaking (ES Modules)

// ✅ Good: Import only what you need
import { format } from 'date-fns';
 
// ❌ Bad: Import the whole library
import _ from 'lodash'; 
// Use instead: import debounce from 'lodash/debounce';

Why It Matters

  • Main Thread Blocking: JavaScript execution happens on the main thread, blocking user interactions.
  • Device Disparity: A 1MB script might take 1s to parse on a flagship phone but 10s on a budget device.
  • Data Usage: Larger files consume more of the user's data plan, which is critical for users on metered connections.
  • SEO Impact: Slow interaction metrics (FID/INP) are part of Google's Core Web Vitals and can affect search rankings.

Best Practices

Code Splitting: Break large bundles into smaller, page-specific chunks. ✅ Tree Shaking: Ensure your build tool (Webpack, Vite, Rollup) is removing unused exports. ✅ Audit Dependencies: Use tools like bundle-analyzer to find "heavy" libraries. ✅ Compression: Always serve JS with Gzip or Brotli compression.

Tools & Validation

Verification

Automated Checks

  • Measure the affected page or flow in Lighthouse, PageSpeed Insights, or DevTools and confirm the targeted metric improves.
  • Inspect the network waterfall or performance timeline to confirm the intended resource or execution change actually took effect.

Manual Checks

  • Verify the change on a throttled mobile profile, not just local desktop.
  • If this rule maps to a budget or Web Vital, confirm the page now stays within that threshold.

Use with AI

Copy these prompts to use with your AI assistant, or install the MCP server to use directly from Claude, Cursor, or Windsurf.

Check

Verify implementation

Review the JavaScript bundle sizes and identify any files exceeding 200KB (Gzip/Brotli).

Fix

Auto-fix issues

Implement code-splitting, tree-shaking, and dependency audits to reduce the size of the JavaScript bundles.

Explain

Learn more

Explain how large JavaScript files impact the main thread and delay user interactivity (FID/TTI).

Review

Code review

Review the routes, assets, and loading behavior that affect Optimize JavaScript bundle size. Flag exact files, requests, or rendering steps that add unnecessary network, CPU, or layout cost, and describe the measurement method used to confirm the issue.

Sources

References used to support the guidance in this rule.

Further Reading

Tools and supplementary material for exploring the topic in more depth.

PageSpeed Insights
pagespeed.web.devTool

Rules that often go hand-in-hand with this one.

Keep page weight under 1500KB

Total page weight including all resources is under 1500KB (ideally under 500KB).

Performance
Optimize CSS file size

Keep individual CSS files small and remove unused styles to accelerate the critical rendering path.

Performance
Enable text-based compression

Compress text resources (HTML, CSS, JS) using Gzip or Brotli to reduce data transfer size.

Performance
Minimize HTTP requests

HTTP requests are minimized by combining files, using sprites, and HTTP/2.

Performance

Was this rule helpful?

Your feedback helps improve rule quality. This stays internal for now.

Loading feedback...
0 / 385