Try it Live
Run Base64 examples in the interactive playground
BrandedBase64
Tree-shakeable functional API for Base64 operations with optimal bundle size.Overview
BrandedBase64 is the functional layer providing base64 encoding/decoding operations. It offers:
- Tree-shakeable individual function exports
- Data-first unopinionated methods
- Bundle optimization through selective imports
- Zero dependencies on Web APIs (
btoa/atob)
Available Functions
All Base64 functionality available as tree-shakeable functions:Standard Encoding/Decoding
URL-Safe Encoding/Decoding
Validation
Utilities
Data-First Pattern
All BrandedBase64 functions follow data-first pattern:Tree-Shaking Benefits
Primary benefit: Selective inclusion of encoding variantsExample 1: Standard Only (Minimal)
Example 2: URL-Safe Only
Example 3: With Validation
Example 4: Complete (All Features)
Function Reference
Encoding Functions
| Function | Input | Output | Notes |
|---|---|---|---|
encode | Uint8Array | Base64String | Standard base64 with padding |
encodeString | string | Base64String | UTF-8 string to standard base64 |
encodeUrlSafe | Uint8Array | Base64UrlString | URL-safe, no padding |
encodeStringUrlSafe | string | Base64UrlString | UTF-8 string to URL-safe base64 |
Decoding Functions
| Function | Input | Output | Notes |
|---|---|---|---|
decode | string | Uint8Array | Standard base64 to bytes |
decodeToString | string | string | Standard base64 to UTF-8 string |
decodeUrlSafe | string | Uint8Array | URL-safe base64 to bytes |
decodeUrlSafeToString | string | string | URL-safe base64 to UTF-8 string |
Validation Functions
| Function | Input | Output | Notes |
|---|---|---|---|
isValid | string | boolean | Validate standard base64 format |
isValidUrlSafe | string | boolean | Validate URL-safe base64 format |
Utility Functions
| Function | Input | Output | Notes |
|---|---|---|---|
calcEncodedSize | number | number | Calculate encoded string length |
calcDecodedSize | number | number | Calculate max decoded byte length |
Usage Patterns
Minimal Bundle (Basic Encoding)
With Validation
URL-Safe for Web APIs
Size Pre-calculation
When to Use BrandedBase64 vs Base64
Use BrandedBase64 When:
- Bundle size critical (mobile, embedded)
- Selective imports desired
- Only using specific variants (standard OR URL-safe)
- Functional style preferred
- Composing functions heavily
Use Base64 Namespace When:
- Using multiple variants
- Ergonomics over bundle size
- Simple namespace imports preferred
- All features needed anyway
Interoperability
BrandedBase64 functions work with Base64 namespace:Types
Implementation Details
All BrandedBase64 functions are implemented using Web APIs:encodeusesbtoa()for encodingdecodeusesatob()for decoding- URL-safe variants apply character substitutions
- No external dependencies
Performance
BrandedBase64 functions have same performance as Base64 namespace (same implementations):- Encoding: O(n) where n is input byte length
- Decoding: O(n) where n is input string length
- Validation: O(n) with early exit on invalid chars
- Size calculation: O(1) pure math
Bundle Size Comparison
Approximate gzipped sizes:| Import Pattern | Size | Includes |
|---|---|---|
{ encode, decode } | ~200B | Standard encode/decode only |
{ encodeUrlSafe, decodeUrlSafe } | ~250B | URL-safe variants only |
{ encode, decode, isValid } | ~400B | Standard + validation |
{ encode, decode, calcEncodedSize } | ~250B | Standard + size calc |
| All functions | ~800B | Complete Base64 implementation |
Base64 namespace | ~800B | Same as all functions |
Related
- Encoding - Encoding operations
- Decoding - Decoding operations
- Validation - Format validation
- Utilities - Size calculations
- Base64 - Main Base64 namespace documentation
- Branded Types - Type branding pattern

