What are Some Alternatives to Using Base64 Encoding?

Alternatives to base64 encoding can be better options for some use cases.
On this page

What are Some Alternatives to Using Base64 Encoding?

Excerpt

Besides base64 encoding, options like Base16, Data URIs, image URLs, and binary storage may be better for specific use cases involving binary data.


Base64 is commonly used to encode binary data like images into text for transmission or storage. But it has some downsides like larger encoded size. Are there better alternatives? In this post, we’ll look at some options besides base64 encoding and when to use them.

Brief Background on Base64 Encoding

First a quick recap of how base64 encoding works. It converts binary data into text by mapping 6-bit binary segments to 64 printable characters. This allows encoding binaries like images into strings.

Base64 is most commonly used for:

  • Embedding small images directly in code or markup.
  • Transferring binary data through text-only channels.
  • Storing binary data like images in text database fields.

Limitations of Base64 Encoding

However, some downsides to base64 encoding include:

  • Encoded output can be around 33% larger than the original binary input.
  • Extra processing overhead for encoding and decoding.
  • Not optimized for storing larger binary objects like files.

So in many cases, there may be better alternatives than base64.

Alternative 1: Reference Image URLs Instead of Embedding

For embedding images in HTML, CSS, etc. consider referencing image files instead of encoding them into base64 strings.

For example:

1<img src="images/logo.png" />

vs:

1<img src="data:image/png;base64,iVBORw0..." />

This keeps image data separate in optimized binary files. It also allows specifying image dimensions for responsive sizing:

1<img src="logo.png" width="100" height="100" />

The tradeoff is you must manage the separate image files.

Alternative 2: JSON Binaries Using Base16 Encoding

For storing binary data in JSON documents and databases, consider Base16 encoding. It uses hex digits 0-9 and A-F to encode each 4-bit segment of binary data.

Compared to base64, Base16 results in smaller encoded string size. But it is less generic - mainly used for JSON use cases.

Alternative 3: Data URIs for Inlining Binaries

Data URIs allow inlining small binaries using a mimetype and base64 encoding:

data:[<media type>][;base64],<data>

For example:

data:image/png;base64,iVBORw0...

Data URIs are well supported across languages and frameworks. The metadata provides context on the binary data type.

Alternative 4: Optimized Binary Storage

For larger files and binaries, store them in an optimized binary format rather than converting to text. Most databases have Binary Large Object (BLOB) storage suitable for files.

This provides much better performance than encoding, at the cost of schema changes.

When to Use Base64 Encoding

Given the alternatives, when does base64 encoding still make sense?

  • For small inline images and binaries when you can’t use separate files.
  • Only when the other options won’t work for technical or project reasons.

In other cases, the alternatives will likely be better choices.

Summary - Base64 Isn’t Always the Best Encoding

In summary, base64 encoding is not the only option for handling binary data like images as text. Other encodings like Base16 or Data URIs are optimized for specific use cases. Storing binaries natively can be better for large files and attachments.

Consider the tradeoffs like encoded size versus support when choosing an encoding. Use the right encoding for your specific needs rather than defaulting to base64 in all cases. With emerging standards like Data URIs and Base16, base64 may make sense less often unless you need generic encoding.

Understanding the options allows picking the best solution for your images and binary data requirements.