Skip to main content

Try it Live

Run Base64 examples in the interactive playground

Utilities

Utility functions for calculating base64 encoding and decoding sizes.

Size Calculation Functions

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) * 4
Parameters:
  • dataLength: number - Length of data to encode in bytes
Returns: number - Size of base64 output in charactersExample:
Defined in: primitives/Base64/BrandedBase64/calcEncodedSize.js:7Algorithm:
  • Every 3 bytes encode to 4 base64 characters
  • Partial groups padded to 4 characters
  • Result always multiple of 4

Base64.calcDecodedSize(encodedLength)

Calculates maximum size of decoded output given base64 string length. Useful for pre-allocating buffers before decoding.Formula: Math.floor((encodedLength * 3) / 4) - padding
Parameters:
  • encodedLength: number - Length of base64 string in characters
Returns: number - Maximum size of decoded output in bytesExample:
Defined in: primitives/Base64/BrandedBase64/calcDecodedSize.js:7Algorithm:
  • 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: