Lesson 8 of 10
Bundle Analysis & Tree Shaking
A large JavaScript bundle = slower startup. Analyzing the bundle shows which libraries consume the most bytes so you can tree-shake, defer, or replace them.
Analyze Metro Bundle
# ---- Metro bundle analysis ----
# Build with bundle stats
npx expo export --platform android --dev false --output-dir dist
# Visualize with Expo Atlas (Expo SDK 51+)
npx expo-atlas dist/
# Or with react-native-bundle-visualizer
npm install --save-dev react-native-bundle-visualizer
npx react-native-bundle-visualizer
# Opens an interactive treemap in your browser
# Each rectangle = one module, sized by bytes
# ---- Common bundle fat culprits ----
# moment.js (300KB) → use dayjs (7KB) or date-fns with tree-shaking
# lodash (full, 500KB) → import lodash/cloneDeep (single function)
# @expo/vector-icons whole set → only import what you need:
// ❌ Imports every icon (adds ~200KB)
import { Ionicons } from '@expo/vector-icons';
// ✅ Import only from the specific icon family you use
import Ionicons from '@expo/vector-icons/Ionicons';
// Each icon family is a separate lazy-loaded asset
# ---- Check each library's actual bundle size ----
# bundlephobia.com - paste any npm package name
# Shows "minified + gzipped" size - use this for comparisonTip
Tip
Practice Bundle Analysis Tree Shaking in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Analyzing JS bundle composition, removing dead code, and lazy loading modules
Practice Task
Note
Practice Task - (1) Write a working example of Bundle Analysis Tree Shaking from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Bundle Analysis Tree Shaking is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?