Why does Base64 make files about a third bigger?

Dark navy cover card with the title 'Why does Base64 make files about a third bigger?' in white and a green accent bar

The Base64 size increase is about 33% because Base64 stores only 6 bits in each output character, while each input byte holds 8 bits. Every 3 bytes (24 bits) therefore become 4 characters, so the output is 4/3 the size of the input. Padding on short inputs and line breaks in email push the overhead slightly higher.

Why Base64 causes a size increase

Base64 grows data because it trades density for safety: it uses a small alphabet of printable characters that every text channel passes through unchanged. RFC 4648, the standard that defines Base64, says it uses "A 65-character subset of US-ASCII", "enabling 6 bits to be represented per printable character".

A byte has 8 bits, but a Base64 character carries only 6. Each character still takes a full byte to store or send. So 2 of every 8 bits in the output are spent on making the data printable, not on the data itself.

MDN's Base64 glossary puts the math in one line: three 8-bit bytes "(3×8 bits = 24 bits) can be represented by four 6-bit Base64 digits (4×6 = 24 bits)."

The 3-bytes-to-4-characters math, step by step

The encoder reads the input in 24-bit groups and writes 4 characters for each group. RFC 4648 states this directly: "The encoding process represents 24-bit groups of input bits as output strings of 4 encoded characters."

Take the 3 bytes of the word Man:

  1. The bytes are M (77), a (97) and n (110), 24 bits in total.
  2. The encoder splits those 24 bits into four groups of 6 bits.
  3. Each 6-bit group is a number from 0 to 63, which maps to one letter, digit, + or /.
  4. The result is TWFu: 4 characters for 3 bytes.

The older MIME standard, RFC 2045, puts it plainly: the encoded data are "consistently only about 33 percent larger than the unencoded data."

Padding makes small inputs grow more

Padding adds = characters when the input length is not a multiple of 3, so short inputs grow by more than a third. RFC 4648 says a final group of 8 bits ends in two characters plus ==, and a final group of 16 bits ends in three characters plus =.

I measured real output lengths with Node's Buffer.toString('base64'):

Input bytes Base64 characters Growth
1 4 4x
3 4 1.33x
4 8 2x
100 136 1.36x
1000 1336 1.336x
1,000,000 1,333,336 1.333336x

The formula behind the table is 4 × ceil(n / 3), where n is the number of input bytes. MDN notes that the exact increase depends on "its length modulo 3, and whether padding characters are used". Base64URL often drops the padding, which saves up to 2 characters; see Base64 vs Base64URL for when that applies.

Line breaks push email attachments to about 37%

Line breaks add a few more percent on top of the 33% when the encoder wraps its output. RFC 2045 requires MIME email to keep encoded lines to "no more than 76 characters each", and every line ends with a carriage return and line feed.

Wikipedia's Base64 article works this out as "usually about 137% of the original data length (4/3×78/76)". My test agreed: 1,000,000 random bytes wrapped at 76 characters came to 1,368,422 bytes, or 1.368422x. That is why a 10 MB attachment often hits a mail server's limit sooner than you expect.

Outside email, line breaks are not part of the format. RFC 4648 says implementations "MUST NOT add line feeds" unless another spec tells them to. JSON APIs, data URIs and JWTs carry one unbroken line.

Does gzip or Brotli cancel the overhead?

Compression removes most of the Base64 overhead for random binary data, but not for text. I compressed both forms with Node's zlib.

Data Original compressed Base64 compressed
1,000,000 random bytes (gzip) 1,000,328 1,004,227
1,000,000 random bytes (Brotli) not measured 1,000,104
900,000 bytes of repetitive text (gzip) 2,696 3,588

For random bytes, which behave like JPEG, PNG or zip content, gzip squeezed the Base64 back to within about 0.4% of the original. The compressor sees only 64 distinct symbols and packs them close to 6 bits each.

Text goes the other way. The gzipped Base64 stayed about a third larger than the gzipped original, because Base64 breaks up the repeated byte patterns that gzip looks for.

My take: if your server compresses responses, a Base64 image inside JSON costs less than the 33% headline suggests. Base64-encoding text before you send it is a waste: you pay the overhead and lose much of the compression.

When the extra third actually matters

The Base64 overhead matters most where size limits or caching apply, not on small values.

  • Data URIs in HTML or CSS. MDN's data URL page says browsers accept large ones, with Chromium and Firefox at 512MB. The image still downloads with the page every time, inflated by a third.
  • JSON APIs that return files. A 3 MB PDF becomes a 4 MB string field, and the client must hold both copies in memory to decode it.
  • Email attachments, where the 37% figure above counts against the size limit.
  • Tokens in headers and URLs, where every extra character counts toward header limits.

The limitation of Base64 is built in: no setting or library makes it smaller than 4/3, because the alphabet size fixes it. If size is the problem, send raw bytes over a binary channel such as a normal file upload instead of encoding them.

Base64 also does nothing for privacy, as Is Base64 encryption? explains. To measure your own data, the OnlineViewer Base64 encoder shows the encoded output in your browser without uploading it. For text with emoji or accents, check how to Base64 encode Unicode in JavaScript, since UTF-8 can make the input itself larger before Base64 adds its third. And to inspect encoded data in a terminal, see how to decode Base64 on the command line.

FAQ

How do I calculate the Base64 size of a file?

Divide the byte count by 3, round up, and multiply by 4. A 100-byte file becomes 136 characters, and 1,000,000 bytes become 1,333,336 characters, before any line breaks.

Is Base64 always exactly 33% bigger?

No. Small inputs grow more because of padding: 1 byte becomes 4 characters. Large inputs approach 4/3 of the original, and MIME line breaks push email attachments to about 137%.

Does gzip remove the Base64 overhead?

Mostly for random binary data such as images or zip files, where my test left about 0.4% extra. For compressible text the compressed Base64 stayed about a third larger than the compressed original.

Why not use a more efficient encoding than Base64?

Base64 uses only 64 safe printable characters so it survives email, JSON, URLs and headers. Denser encodings need characters that those channels may change or reject.

Sources