UPD. Charts updated with better input data.
Recently I stumbled upon some old code that used Base64 from Apache Commons. So I decided to compare its performance to java.util.Base64.
It’s not a new topic whatsoever (links at the end), so let’s just jump to benchmark results.
Benchmarks
Performance for encoding 10K characters for different JDKs. java.util.Base64
is a clear winner performing almost 10x faster.
Performance for different data sizes (from 1 byte to 10K bytes) for openjdk-17. As you could see in the previous chart, openjdk-8 performs worse, but still java.util.Base64
is clearly better in all cases.
Conclusion
It’s clearly a time to replace old usages of Apache Commons for base64 encoding. Unless you are on JDK7 or lower, then it’s time to upgrade this part first ;-) (as java.util.Base64
was introduced in JDK8)
Read More
- Base64 encoding and decoding performance: a big good article from 2014 with multiple libraries comparison. Site doesn’t exist anymore, so link to web archive.
- A Fast and Correct Base 64 Codec: AWS’s take on base64 (spoiler, their implementation is more “correct”).
- Follow up on Base64 Codec Performance: AWS’s performance benchmark.
- Java Base64 Encoding and Decoding: basic tutorial.
Play with charts here. Source code is on GitHub. Originally posted on Medium. Cover image by mohamed_hassan from Pixabay.