Try it Live
Run Base64 examples in the interactive playground
Utilities
Utility functions for calculating base64 encoding and decoding sizes.Size Calculation Functions
- TypeScript
Base64.calcEncodedSize(dataLength)
Calculates size of base64-encoded output given input data length. Useful for pre-allocating buffers before encoding.Formula: Math.ceil(dataLength / 3) * 4dataLength: number- Length of data to encode in bytes
number - Size of base64 output in charactersExample:- Every 3 bytes encode to 4 base64 characters
- Partial groups padded to 4 characters
- Result always multiple of 4
Base64.calcDecodedSize(encodedLength)
- TypeScript
Calculates maximum size of decoded output given base64 string length. Useful for pre-allocating buffers before decoding.Formula: Parameters:Defined in: primitives/Base64/BrandedBase64/calcDecodedSize.js:7Algorithm:
Math.floor((encodedLength * 3) / 4) - paddingencodedLength: number- Length of base64 string in characters
number - Maximum size of decoded output in bytesExample:- Every 4 base64 characters decode to 3 bytes
- Returns maximum size (actual may be smaller due to padding)
- For exact size, must decode and check
Usage Patterns
Pre-allocating Buffers
Memory Estimation
Batch Processing with Size Limits
Validation with Size Checks
Size Optimization
Stream Processing
Size Calculation Reference
Encoding Size Examples
Base64 encoding increases size by approximately 33% (4/3 ratio).
Decoding Size Examples
Base64 decoding reduces size by 25% (3/4 ratio).
Mathematical Properties
Encoding Formula
Decoding Formula
Size Relationships
Performance
Size calculations are pure mathematical operations with O(1) complexity:Related
- Encoding - Encoding to base64 format
- Decoding - Decoding base64 strings
- Validation - Validating base64 format
- BrandedBase64 - Tree-shakeable API

