Happy Time

Base64 costs 33 percent more bytes. Here is when it is still the right call

3 min read

Base64 is neither compression nor encryption. It exists to carry binary data through text-only channels. Understanding the 33 percent overhead, where the line breaks come from, and the break-even point for inlining images tells you when to use it.

Where the 33 percent comes from

Base64 takes every 3 bytes (24 bits), splits them into four 6-bit groups, and maps each group to one of 64 printable characters. Three bytes become four characters, so the overhead is exactly 4/3, or 33.3 percent. When the input length is not a multiple of three, padding characters are appended. The encoded form is therefore always larger than the original — anything advertised as "Base64 compression" is wrong. The value is not size, it is that the result survives channels that mangle arbitrary bytes: email bodies, JSON strings, XML, CSS and URLs.

Three cases where it is the right tool

One criterion: the data has to pass through a text-only channel.

  • Inlining small icons or fonts into CSS or HTML to save a request. Only worth it for files of a few KB.
  • Carrying attachments, signatures or key material inside JSON or XML, which have no notion of a binary field.
  • Email attachments, HMAC signatures in APIs, JWT payload segments — protocol requirements with no alternative.

Three cases where it hurts

Large files first. A 2 MB image becomes 2.7 MB of text, that text ships with the HTML, cannot be cached on its own, and blocks first paint. Images belong in an img tag with normal HTTP caching. Second, anything that needs independent caching: an image inlined into CSS invalidates with the whole stylesheet, so changing one small icon forces a re-download of everything. Third, data that will be compressed anyway: the base64 text is low-entropy and gzips reasonably well, but the gzipped result is still bigger than gzipping the raw bytes.

Where those line breaks come from

Many tools wrap output at 76 characters. That rule comes from MIME (RFC 2045) for email bodies, where old terminals and SMTP servers limited line length. Modern contexts — JSON fields, URL parameters, string literals in code — usually want one continuous line, so the wrapping becomes a problem to strip. When decoding, tolerate wrapped lines, stray spaces, missing padding, and the URL-safe alphabet that swaps + and / for - and _. Those four "non-standard but everywhere" inputs cause most decode failures; the data is usually fine.

The encryption misunderstanding

Base64 is reversible encoding with no key: anyone holding the string can decode it. It is often mistaken for "light encryption" and used to "protect" passwords or ID numbers in internal systems, where it protects nothing — it only makes the data stop looking like plain text. Use real encryption with managed keys when confidentiality matters, and be explicit about the weaker guarantee when you only need obfuscation. Related: a JWT payload is base64url encoded and readable by anyone; the signature protects integrity, not secrecy.

Common questions

How does Base64 differ from Base32 or hex?
The alphabet and bits per character. Base64 uses 64 symbols at 6 bits each for 33 percent overhead; Base32 uses 32 symbols at 5 bits for 60 percent; hex is 4 bits per character for 100 percent. Choose by which characters the channel accepts (case-insensitive systems need Base32) and how much the overhead matters.
Why do two tools produce different Base64 for the same file?
Three usual causes: different line-wrapping (76, 64 or none), the URL-safe alphabet, and whether trailing padding is kept. All decode to identical bytes; only the spelling differs. Compare after decoding, not as strings.
When is inlining an image worth it
Roughly under 10 KB per image and effectively immutable (logos, icons, a critical above-the-fold graphic). Above that, a separate request with a long cache lifetime usually wins, especially over HTTP/2 where parallel requests are cheap.

Read next

Base64 costs 33 percent more bytes. Here is when it is still the right call · Happy Time