Technology3 min read

Decoding base64 in Python at 10 gigabytes per second

By · Published by Everything Blog

In short

Base64 is a common method for encoding binary data into text, where three bytes are represented by four ASCII characters. This encoding is used in various applications, including email attachments, digital certificates, and image data within web pages. Python has a built-in module for base64 encoding and decoding, but it processes data one character at a time.

Key points

  • Base64 is a common method for encoding binary data into text, where three bytes are repre…: Base64 is a common method for encoding binary data into text, where three bytes are represented by four ASCII characters.
  • This encoding is used in various applications, including email attachments, digital certi…: This encoding is used in various applications, including email attachments, digital certificates, and image data within web pages.
  • Python has a built-in module for base64 encoding and decoding, but it processes data one…: Python has a built-in module for base64 encoding and decoding, but it processes data one character at a time.

Sources checked by our analysis pipeline: 1

17 October 2026 · 3 min

We sometimes need to convert binary data to text. For example, your emails must be stored as text files. Naïvely, you could just use a string of 0

and 1

, but it would mean using one character per bit. We can do better by using 64 ASCII characters: all letters, all numbers, plus a few extra characters. Base64 represents three bytes with four ASCII characters.

Base64 is how email carries attachments, how certificates are stored, how images get embedded in web pages (data URLs), and how binary data is encoded inside JSON.

Python comes with a base64

module. It is written in C, but it processes the data one character at a time. There is a popular alternative, pybase64.

We have been working on base64 for years. With Wojciech Muła, we showed how to encode and decode base64 with SIMD instructions. More recently, I worked on forgiving base64 decoding: the variant used by web browsers, which skips spaces and line breaks. Our simdutf library implements these ideas. Mainstream browsers rely on it.

But simdutf had no Python binding. So we wrote one: fastpybase64.

pip install fastpybase64

import fastpybase64 as base64

base64.b64encode(b"hello") # b'aGVsbG8='

base64.b64decode("aGVsbG8=") # b'hello'

It is meant as a drop-in replacement for the standard base64

module.

How fast is it? I use an Intel Xeon Gold 6548N (Emerald Rapids) pinned to one core, with Python 3.14.6. I installed fastpybase64 0.1.0 and pybase64 1.5.1 from PyPI. I decode the base64 encoding of random bytes, and I report the speed in gigabytes of decoded data per second. A lot of real base64 has line breaks. MIME, the format of email attachments, breaks the base64 text into lines of 76 characters. So that’s what I use as an example. We use a single thread.

The fastpybase64 module is four times faster than pybase64 and 27 times faster than the standard library.

The fastpybase64 module is available on PyPI for Linux, macOS and Windows, for Python 3.10 to 3.14, including free-threaded Python 3.14. My benchmark is available.

Further reading

- Daniel Lemire, Fast Forgiving Base64 Decoding with SIMD Instructions, ACM Transactions on Architecture and Code Optimization, 2026.

- Wojciech Muła, Daniel Lemire, Base64 encoding and decoding at almost the speed of a memory copy, Software: Practice and Experience 50 (2), 2020.

- Wojciech Muła, Daniel Lemire, Faster Base64 Encoding and Decoding using AVX2 Instructions, ACM Transactions on the Web 12 (3), 2018.

Daniel Lemire, "Decoding base64 in Python at 10 gigabytes per second," in Daniel Lemire's blog, October 17, 2026, https://lemire.me/blog/2026/10/17/decoding-base64-in-python-at-10-gigabytes-per-second/.

[BibTeX]

You can also subscribe by email to this blog (non-commercial, no ads, weekly email).

How to post code (C, C++, Java, Python, etc.):

Wrap your code in backticks, like this:

Original source: lemire.me

Technology