This package provides a base64 encoder and decoder in Swift without the use of Foundation. The implementation is RFC4648 complient and is faster than the Foundation base64 implementation.
To achieve performance the implementation uses Chromium precomputed lookup tables and makes heavy use of unsafe swift API. When Swift has better support for SIMD instructions this might be an area worth exploring.
- ☑ support for base64 and base64url
- ☑ faster than Foundation
- ☑ padding can be omitted
- ☐ decoding can ignore line breaks
- ☐ encoding can insert line breaks
A former implementation of this package is used in
swift-nio's websocket implementation.
Super simple performance test to ensure speediness of this implementation. Encoding and decoding 1m times the base64 string:
Tests were run on a MacBook Pro (16-inch, late 2019). Processor: 2.4 GHz 8-Core Intel Core i9.
macOS - swift 5.3
Linux - swift 5.3
Literature for a faster algorithm
I would really like to speed up this project further to be way faster than it is today. Some food for thought of how this could be tackled can be found here:
- Wojciech Muła, Daniel Lemire: Faster Base64 Encoding and Decoding using AVX2 Instructions.
- Daniel Lemire's blog - Ridiculously fast base64 encoding and decoding
- Swift SIMD support
As of today (2019-12-10), the author is aware of only one alternative that offers merely encoding.
You may find interesting
swift-extras-base64 v0.4.0 -
swift-base64-kit was moved to swift-extras and got a new name:
Base64Kit v0.3.0 -
- Faster encode and decode thanks to Chromium precomputed lookup tables #19
Base64Kit v0.2.0 -
- new name (#6 #7)
- some code cleanup with @weissi's help (#4)
- removed last use of
Base64 0.1.0 -
The very first release! Feedback highly welcome.