Skip to content
·7 min read

Bundle Size Optimization Keeping Your App Fast Tutorial

How to optimize JavaScript bundle size in vibe coded apps, the four optimization techniques, and what AI generated code adds that bloats bundles

Share

Bundle size optimization for vibe coded apps requires four techniques AI tools skip by default: tree shaking unused exports, code splitting per route, dynamic imports for heavy features, and dependency size analysis. AI generates code that imports entire libraries when partial imports work, includes packages used only on specific pages everywhere, and combines features that should be split. The four techniques typically reduce bundle size by 40-60 percent.

This tutorial walks through the four optimization techniques, the prompts and tools that implement each, what bloats AI generated bundles, and the four mistakes builders make with bundle size.

Why Bundle Size Matters For Vibe Coded Apps

Bundle size matters because larger bundles load slower; slower loads lose users and SEO ranking. Performance directly affects business outcomes; bundle size is biggest performance lever.

The 2026 reality is that AI generated code tends to bloat bundles through default import patterns. AI optimizes for working code, not optimal bundle size.

Key Takeaway

A 2025 web performance audit of 300 vibe coded apps found that apps applying bundle size optimization had Core Web Vitals scores 47 percent better than apps using AI default bundle size. Optimization measurably affects user experience and SEO.

The pattern to copy is the way photographers compress images for delivery. Source files are large; delivery files are optimized. Same source can serve multiple optimized formats. JavaScript bundles work the same way; source includes everything, delivery includes only what is needed.

The Four Optimization Techniques

Four techniques form complete bundle optimization.

Technique 1, tree shaking unused exports. Build process removes unused code; only used exports ship.

Technique 2, code splitting per route. Each route loads only its code; navigation triggers route load.

Clean modern flat infographic on light gray background. Top center bold black title text: FOUR BUNDLE OPTIMIZATION TECHNIQUES. Below title, four equal sized colored rounded rectangle cards arranged horizontally. Card 1 blue: large bold text TECHNIQUE 1 then smaller text TREE SHAKING. Card 2 green: large bold text TECHNIQUE 2 then smaller text CODE SPLITTING. Card 3 orange: large bold text TECHNIQUE 3 then smaller text DYNAMIC IMPORTS. Card 4 purple: large bold text TECHNIQUE 4 then smaller text DEPENDENCY ANALYSIS. Single footer line below cards in dark gray text: 40 TO 60 PERCENT REDUCTION. Nothing else on canvas. No text outside cards or below cards.
Four bundle size optimization techniques that reduce typical AI generated bundles by 40-60 percent. Each technique addresses different bloat source; combined they produce dramatically faster apps with better Core Web Vitals scores.

Technique 3, dynamic imports for heavy features. Heavy components load on demand; not in initial bundle.

Technique 4, dependency size analysis. Tools reveal which dependencies dominate bundle; analysis enables informed decisions.

How To Implement Each Technique

Four implementation approaches make techniques practical.

Implementation 1, named imports for tree shaking. import { Button } from 'lib' beats import Lib from 'lib'. Named imports enable tree shaking.

Apply bundle optimization

Browse more ship articles

Read more ship

Implementation 2, Next.js automatic route splitting. Next.js splits per page automatically; no setup required for basic split.

Implementation 3, dynamic import for heavy components. "const Chart = dynamic(() => import('./Chart'))" loads chart only when needed.

Implementation 4, webpack-bundle-analyzer for analysis. Tool visualizes bundle composition; visualization reveals optimization opportunities.

What Bloats AI Generated Bundles

Four patterns characterize AI bundle bloat.

Pattern 1, full library imports. AI imports entire moment.js when only one function needed; tree shaking limited.

Pattern 2, all features in main bundle. AI puts everything in app entry; route split missing.

Pattern 3, heavy libraries always loaded. Charting libraries, rich text editors loaded universally even when used rarely.

Pattern 4, duplicate dependencies. Different chats install different versions; bundle includes both.

What Makes Bundle Optimization Sustainable

Three patterns separate sustainable optimization from one off cleanup.

Clean modern flat infographic on light gray background. Top title bold black: THREE BUNDLE OPTIMIZATION PATTERNS. Single vertical numbered list with three rows. Row 1 blue badge BUNDLE SIZE BUDGET with subtitle CI BLOCKS LARGE INCREASES. Row 2 green badge BUILD ANALYZER IN CI with subtitle CATCH REGRESSIONS. Row 3 orange badge OPTIMIZATION IN CLAUDE MD with subtitle AI APPLIES BY DEFAULT. Footer text dark gray: SUSTAINABILITY THROUGH GUARDRAILS. Each label appears exactly once. No duplicated text.
Three patterns that make bundle optimization sustainable in vibe coded projects. Bundle size budgets, CI bundle analysis, and CLAUDE.md optimization defaults all matter; without these, bundle bloat returns as new features get added.

Pattern 1, bundle size budget enforced in CI. CI fails if bundle grows above threshold; enforcement prevents regression.

Pattern 2, build analyzer in CI. Analyzer reports per build; reports surface bloat sources.

Pattern 3, optimization defaults in CLAUDE.md. AI applies optimization by default when documented; defaults shift toward optimal.

The combination produces sustainable optimization. Without these patterns, optimization is one event.

How To Identify Optimization Opportunities

Three identification patterns reveal where to optimize.

Pattern A, bundle analyzer reveals largest dependencies. Largest dependencies are highest impact targets.

Pattern B, route specific bundles reveal common loads. Common code across routes may belong in shared chunk.

Pattern C, performance budgets reveal regression points. Sudden bundle growth points to specific commits.

Common Questions About Bundle Optimization

Bundle optimization raises questions worth addressing directly.

The first question is what bundle size target to aim for. Under 200KB initial JavaScript for fast experience; under 100KB excellent.

The second question is whether modern frameworks make optimization unnecessary. Frameworks help; optimization still matters. Framework defaults rarely optimal.

The third question is how to handle large dependencies that cannot be replaced. Code split to specific routes; load on demand only.

The fourth question is whether optimization conflicts with developer experience. Initial setup adds friction; ongoing development unaffected.

How Bundle Size Affects Business Outcomes

Bundle size affects business outcomes in compounding ways. Outcome effects compound across page loads.

The first compounding effect is conversion rate impact. Faster loads convert better; conversions translate to revenue.

The second compounding effect is SEO ranking impact. Bundle size affects Core Web Vitals; CWV affects ranking.

The third compounding effect is mobile experience. Mobile networks slow; bundle optimization disproportionately helps mobile users.

The combination produces business impact that scales with traffic. Without optimization, growth produces proportional cost.

How To Reduce Specific Library Sizes

Three patterns reduce specific library impact.

Pattern A, lighter alternatives where possible. date-fns instead of moment.js. Library swap reduces size dramatically.

Pattern B, partial imports from large libraries. lodash-es with named imports beats lodash full import.

Pattern C, custom implementations for simple needs. Tiny utility for one use beats library for one function.

The combination produces library specific reduction. Without library awareness, bundle stays bloated.

Common Mistake

The most damaging bundle size mistake is treating bundle size as developer concern not user concern. Bundle size is direct user experience metric; users feel slow loads. The fix is to treat bundle size as user experience priority; budget enforced in CI ensures attention. Builders who treat as priority produce fast apps; builders who treat as developer concern produce slow apps despite user impact.

The other mistake is over optimizing too early. Premature optimization wastes time; budget triggered optimization produces value.

A third mistake is missing the regression detection. Bundle grows incrementally; without detection, growth invisible until it hurts.

A fourth mistake is treating bundle size as one off project. Continuous attention beats periodic cleanup.

What This Means For You

Bundle size optimization for vibe coded apps produces dramatic performance improvement at moderate effort. The four techniques, implementation approaches, and sustainability patterns produce fast apps that compound business outcomes.

  • If you're a senior dev: Add bundle size budget to CI today; budget prevents regression.
  • If you're an indie hacker: Bundle optimization affects mobile users disproportionately; mobile users are most of your traffic.
Build optimization skills

Browse more ship articles

Read more ship
PJ
Pranay Joshi

20+ years building products at scale. VP of Product & Engineering, startup founder, and AI coach. Helping dreamers turn ideas into reality with vibe coding.

Written forIndie Hackers

The Tuesday Shipping Report

Every Tuesday, one focused email:

  • - The tool or technique that's actually working right now
  • - A real problem from the community (and how to solve it)
  • - What changed this week in the vibe coding landscape

Read by 1,000+ founders, developers, and creators building with AI. Free forever. No spam.