Overview
Smaller bundle sizes mean faster app startup, reduced memory usage, and better user experience. This guide shows you how to measure and optimize your app’s bundle size.Measuring Bundle Size
Expo bundle analyzer
- Module sizes
- Dependency tree
- Duplicate packages
- Largest contributors
Manual bundle inspection
Source map explorer
Reducing Bundle Size
Remove unused dependencies
Check for unused packages:Analyze dependency sizes
Replace large dependencies
- Moment.js → date-fns
- Lodash → lodash-es
- Axios → fetch
Tree Shaking
Import only what you need
Configure Metro for tree shaking
metro.config.js
Dynamic Imports
Code splitting with React.lazy
app/_layout.tsx
Conditional imports
Platform-specific code
utils/analytics.ts
Image Optimization
Use WebP format
Image compression
Compress images before adding to your app:Responsive images
components/ResponsiveImage.tsx
Font Optimization
Load fonts selectively
app/_layout.tsx
Use system fonts
Optimizing Vector Icons
Use only needed icon sets
Create custom icon sets
Use SVG instead
Remove Development Code
Use DEV flag
Conditional imports
Hermes Optimization
Hermes provides better performance and smaller bundle size:app.json
- Faster startup time
- Lower memory usage
- Bytecode compilation
- Better optimizations
Production Build Optimization
EAS Build configuration
eas.json
Minification
Metro automatically minifies in production:metro.config.js
Analyzing Dependencies
Find duplicate packages
Check for multiple versions
Use yarn resolutions
package.json
Bundle Size Budget
Set size limits to prevent regressions:bundle-size-check.js
package.json
Best Practices
Import strategy
Lazy loading strategy
Monitoring
Track bundle size over time
.github/workflows/bundle-size.yml
Bundle size dashboard
Use tools like Bundlewatch or RelativeCI to track bundle size across builds.Troubleshooting
Bundle size unexpectedly large
Bundle size unexpectedly large
- Run
npx expo-atlasto analyze - Check for duplicate dependencies with
npm ls - Look for large packages in analysis
- Verify tree shaking is working
Tree shaking not working
Tree shaking not working
- Use named imports, not default imports
- Check package.json has
"sideEffects": false - Ensure using ES modules, not CommonJS
- Verify Metro config is correct
Bundle grew after adding feature
Bundle grew after adding feature
- Check if new dependencies are tree-shakeable
- Look for lighter alternatives
- Consider lazy loading the feature
- Analyze the dependency with bundle-phobia
Size Optimization Checklist
- Run bundle analyzer to identify large modules
- Remove unused dependencies
- Replace large libraries with smaller alternatives
- Use tree-shakeable imports
- Implement code splitting with lazy loading
- Optimize images (WebP, compression)
- Load only required fonts and icon sets
- Enable Hermes engine
- Remove console.log in production
- Enable ProGuard (Android)
- Monitor bundle size in CI
- Set bundle size budgets