Bird
Raised Fist0
Angularframework~8 mins

Bundle size analysis in Angular - Performance & Optimization

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Performance: Bundle size analysis
HIGH IMPACT
This affects the page load speed by determining how much code the browser must download and parse before rendering the app.
Reducing Angular app bundle size for faster initial load
Angular
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule],
  bootstrap: [AppComponent]
})
export class AppModule {}

// Lazy load HeavyLibraryModule only when needed in a feature module or route
Lazy loading the heavy library splits code and loads it only when required, reducing initial bundle size.
📈 Performance GainSaves 500kb on initial bundle, improves LCP by 200-300ms
Reducing Angular app bundle size for faster initial load
Angular
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HeavyLibraryModule } from 'heavy-library';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HeavyLibraryModule],
  bootstrap: [AppComponent]
})
export class AppModule {}
Importing a large library globally increases the main bundle size, delaying app startup.
📉 Performance CostAdds 500kb+ to bundle, blocking rendering for 200-300ms on slow networks
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Large monolithic bundleMinimalMinimalHigh due to delayed rendering[X] Bad
Lazy loaded feature modulesMinimalMinimalLower due to faster initial render[OK] Good
Rendering Pipeline
Bundle size affects the critical rendering path by increasing download, parse, and compile time before the browser can render content.
Network Download
JavaScript Parsing
JavaScript Execution
Style Calculation
Layout
⚠️ BottleneckJavaScript Parsing and Execution
Core Web Vital Affected
LCP
This affects the page load speed by determining how much code the browser must download and parse before rendering the app.
Optimization Tips
1Avoid importing large libraries globally in the root module.
2Use Angular lazy loading to split bundles by feature.
3Analyze bundle size regularly with DevTools Network panel.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance impact of a large Angular bundle?
ASlower initial page load due to longer download and parsing
BMore DOM nodes created
CIncreased CSS selector complexity
DMore layout shifts during animation
DevTools: Network and Performance panels
How to check: Open DevTools, go to Network tab, filter by JS files, check bundle sizes and load times. Use Performance tab to record page load and see scripting time.
What to look for: Look for large JS files delaying first contentful paint and long scripting times blocking rendering.

Practice

(1/5)
1. What is the main purpose of bundle size analysis in Angular applications?
easy
A. To change the app's color scheme
B. To add more features to the Angular app
C. To find which parts of the app make the bundle large
D. To increase the app's loading time

Solution

  1. Step 1: Understand bundle size analysis goal

    Bundle size analysis helps identify what parts of the app increase the size of the final build.
  2. Step 2: Match purpose with options

    Only To find which parts of the app make the bundle large correctly states the purpose as finding large parts in the bundle.
  3. Final Answer:

    To find which parts of the app make the bundle large -> Option C
  4. Quick Check:

    Bundle size analysis = find large parts [OK]
Hint: Think: Why check bundle size? To find big parts [OK]
Common Mistakes:
  • Confusing bundle size analysis with adding features
  • Thinking it changes app appearance
  • Assuming it slows down the app
2. Which Angular CLI command generates a JSON file useful for bundle size analysis?
easy
A. ng build --stats-json
B. ng serve --stats-json
C. ng test --stats-json
D. ng lint --stats-json

Solution

  1. Step 1: Recall Angular CLI commands for building

    The command to build the app and generate stats is ng build --stats-json.
  2. Step 2: Identify correct option

    ng build --stats-json matches the correct command to create the JSON file for bundle analysis.
  3. Final Answer:

    ng build --stats-json -> Option A
  4. Quick Check:

    Build with stats JSON = ng build --stats-json [OK]
Hint: Build command with --stats-json creates analysis file [OK]
Common Mistakes:
  • Using ng serve instead of ng build
  • Confusing test or lint commands with build
  • Forgetting to add --stats-json flag
3. After running ng build --stats-json, you use source-map-explorer on the generated JSON. What will you see?
medium
A. A list of unused CSS styles
B. A list of errors in your Angular code
C. A report of unit test results
D. A visual map showing which files contribute most to bundle size

Solution

  1. Step 1: Understand what source-map-explorer does

    This tool shows a visual breakdown of the bundle, highlighting file sizes.
  2. Step 2: Match output with options

    A visual map showing which files contribute most to bundle size correctly describes the visual map of file size contributions.
  3. Final Answer:

    A visual map showing which files contribute most to bundle size -> Option D
  4. Quick Check:

    source-map-explorer output = visual size map [OK]
Hint: source-map-explorer shows visual file size map [OK]
Common Mistakes:
  • Thinking it shows code errors
  • Confusing with test reports
  • Expecting CSS style reports
4. You ran ng build --stats-json but source-map-explorer shows no data. What is the likely cause?
medium
A. You did not generate the stats JSON file during build
B. You forgot to install source-map-explorer globally
C. You ran ng serve instead of ng build
D. Your Angular app has no components

Solution

  1. Step 1: Check if stats JSON file was created

    source-map-explorer needs the stats JSON file generated by ng build --stats-json.
  2. Step 2: Identify missing stats JSON as cause

    If the file is missing, source-map-explorer shows no data. This matches You did not generate the stats JSON file during build.
  3. Final Answer:

    You did not generate the stats JSON file during build -> Option A
  4. Quick Check:

    No stats JSON = no data in explorer [OK]
Hint: Check if stats JSON file exists before using explorer [OK]
Common Mistakes:
  • Assuming source-map-explorer must be global
  • Confusing ng serve with ng build
  • Thinking app components affect stats generation
5. You notice your Angular bundle is very large. Which combination of actions will best reduce the bundle size?
hard
A. Disable Ahead-of-Time compilation and enable source maps
B. Use lazy loading for modules and remove unused imports
C. Add more third-party libraries and increase polyfills
D. Increase the number of components and services

Solution

  1. Step 1: Identify techniques to reduce bundle size

    Lazy loading loads modules only when needed, and removing unused imports cuts unnecessary code.
  2. Step 2: Evaluate options for reducing size

    Use lazy loading for modules and remove unused imports correctly combines these effective methods. Other options add size or disable optimizations.
  3. Final Answer:

    Use lazy loading for modules and remove unused imports -> Option B
  4. Quick Check:

    Lazy loading + clean imports = smaller bundle [OK]
Hint: Lazy load and clean imports to shrink bundle [OK]
Common Mistakes:
  • Adding more libraries increases size
  • Disabling AOT slows app and increases size
  • Adding components without optimization