ShrinkRay All Articles
Web Performance

Base64’s 33% Tax: When Inlining Becomes an Expensive Habit

By ShrinkRay Web Performance

Base64 is one of those tools that feels free because it is everywhere. You need to stuff an image into a JSON response? Base64. You want a tiny icon directly in CSS? Base64. You are sending a certificate or key in a config file? Base64, probably. It is text-safe, copy-paste-friendly, and supported by everything. It is also about 33% bigger than the bytes it represents.

That is not a scandal. It is arithmetic. Base64 turns every 3 bytes of binary into 4 characters. Add line breaks, padding, and a data URI prefix, and the overhead can creep a little higher. For a 1 MB image, you are shipping roughly 1.37 MB of text before your gzip or Brotli layer does anything. If the image was already compressed — and most images are — that extra third is mostly tax.

ShrinkRay’s position is simple: encode when you need text safety, not because it is the default path of least resistance.

The convenience tax nobody puts on the invoice

Base64 is popular because it solves a real problem. JSON does not have a native binary type. HTML and CSS do not want raw bytes in the middle of a string. Config files are happier as text. Base64 bridges those gaps without requiring multipart form data, signed URLs, or a separate asset pipeline.

But convenience has a way of becoming architecture. A small inline logo becomes a habit. Then someone inlines a hero image. Then an API starts returning avatars as base64 strings. Then a mobile client starts caching giant JSON blobs. Nobody decided to make the payload bigger; it just happened one convenient choice at a time.

What 33% actually looks like

Numbers make the habit easier to see. A 100 KB PNG becomes about 136 KB as base64. A 1 MB PDF becomes about 1.33 MB. A 5 MB video clip becomes roughly 6.7 MB. Those are pre-compression numbers. If your transport compresses the response, gzip or Brotli can knock some of that back down, but they cannot make base64 smaller than the original binary. The overhead is still there, and it still costs bytes on the wire, memory in the client, and time in the parser.

There is also a cache angle. An inline data URI lives inside the HTML, CSS, or JSON that contains it. That means it cannot be cached as a separate asset. If the same 80 KB inline image appears on 50 pages, the browser may cache the document, but the image bytes are still duplicated in every document payload. A separate image URL can be cached once and reused everywhere.

Where base64 earns its keep

Base64 is not a villain. It is a good fit for a few specific jobs.

Small, one-off assets are the classic case. A 1 KB SVG icon inlined into a critical CSS file can save a round trip. A tiny tracking pixel in an email template may need to be a data URI because email clients are strange. A small binary field in a JSON protocol may be simpler as base64 than as a multipart upload, especially for internal tools.

It is also reasonable for config files and protocol fields where text safety matters more than size. A public key in a PEM file is base64 because the format says so. A small opaque token in a JSON response may be base64 because the consumer expects text. Those are intentional choices, not accidental bloat.

Where it quietly gets expensive

Base64 gets ugly when it becomes the default for large or cacheable assets. Inline images and fonts in CSS are common offenders. A 60 KB font subset becomes 80 KB of base64 inside a stylesheet that blocks rendering. A 200 KB JPEG becomes 267 KB inside an HTML document that now takes longer to download and parse.

JSON APIs are another sore spot. Returning image bytes or file contents as base64 fields makes every response larger and every client parse more text. It also makes pagination, streaming, and partial updates harder. If you need to move binary data between services, consider multipart uploads, object storage with signed URLs, or a binary protocol. If you must use JSON, keep the base64 fields small and separate from the hot path.

A simple decision rule

Here is a rule of thumb that works for most web teams. Inline an asset only when it is small, used once, and on the critical path. A 1–2 KB threshold is a sane starting point for data URIs. If the asset is larger than that, or if it appears on more than one page, or if it changes independently of the document, give it a URL.

For JSON APIs, avoid base64 for anything that could be a file reference. A signed URL is usually smaller, cacheable, and easier to reason about. For config files, follow the format. Do not base64 a 2 MB blob into an environment variable and call it configuration.

Benchmarks you can run yourself

You do not need a lab. Take your five largest inline assets and measure the document size with and without them. Run the response through gzip and Brotli. Check whether the inline asset is duplicated across pages. Look at your CDN logs for the same image being downloaded inside different HTML documents. The results are usually boring and convincing: separate, cacheable assets win once they get past a few kilobytes.

The ShrinkRay take

Base64 is an encoding, not a compression strategy. It makes binary data safe for text channels, and it charges a third of the payload for the privilege. That is a fair trade for a small icon in a critical stylesheet. It is a bad trade for a hero image, a font file, or an API response that ships the same bytes over and over.

Use base64 when the text-safety benefit is real and the asset is small. Everywhere else, reference the asset, cache it properly, and let the browser do what it is good at. Your payloads will shrink, your cache hit rate will improve, and nobody will miss the extra third.