Skip to content

Bundle Size & Tree-shaking

Optimize your bundle size by choosing the right approach for your use case.

  • Best for: Static flags known at build time
  • Bundle impact: ~950 bytes per flag
  • Tree-shaking: ✅ Excellent
  • Loading: Synchronous, instant

View detailed usage with code examples →

  • Best for: Dynamic flags with minimal bundle
  • Bundle impact: ~2 KB (component only)
  • Tree-shaking: ✅ Perfect
  • Loading: Async from CDN

View detailed usage with code examples →

  • Best for: CDN-restricted environments, offline apps
  • Bundle impact: ~600 KB (all flags bundled)
  • Tree-shaking: ❌ None
  • Loading: Synchronous, instant

View detailed usage with code examples →

MethodBundle SizeTree-shakingLoadingUse Case
Named imports (3 flags)~2.8 KB✅ ExcellentInstantKnown flags at build time
Named imports (10 flags)~9.5 KB✅ ExcellentInstantCommon flags header
CircleFlag~2 KB✅ PerfectAsync CDNDynamic flags, minimal bundle
DynamicFlag~600 KB❌ NoneInstantCDN-restricted, all flags needed

Code Splitting

Split flag components across routes to load only what’s needed per page.

Lazy Loading

Use React’s lazy() to defer loading flags until they’re actually rendered.

Static Analysis

Run bundle analysis regularly to catch unexpected size increases.

Use CircleFlag

For dynamic flags, prefer CircleFlag over DynamicFlag to avoid bundling unused flags.

Combine named imports for known flags with CircleFlag for dynamic ones:

import { FlagUs } from '@sankyu/react-circle-flags'
import { CircleFlag } from '@sankyu/react-circle-flags'
export function Header({ userCountry }: { userCountry?: string }) {
return (
<div className="flex gap-2">
{/* Static flags: bundled (~950 bytes) */}
<FlagUs width={32} height={32} />
{/* Dynamic flag: loaded from CDN (~0 KB) */}
{userCountry && <CircleFlag countryCode={userCountry} width={32} height={32} />}
</div>
)
}

Total bundle impact: ~2.9 KB (FlagUs + CircleFlag component)

Use these tools to verify your bundle size:

Terminal window
# Webpack Bundle Analyzer
npm install --save-dev webpack-bundle-analyzer
# Vite Bundle Analyzer
npm install --save-dev rollup-plugin-visualizer
# Next.js Bundle Analyzer
npm install --save-dev @next/bundle-analyzer