Skip to content
Base64 encode and decode without the use of Foundation in pure Swift.
Swift
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
Sources
Tests
.codecov.yml
.gitignore
LICENSE
Package.swift
README.md

README.md

swift-base64-kit

Swift 5.1 github-actions codecov macOS tuxOS

This package provides a base64 encoder and decoder in pure Swift (without the use of Foundation). The implementation is RFC4648 complient.

Today the implementation is rather simple. No fancy precomputed lookup tables, no fancy SIMD instructions. Therefore, there is definitely room for improvement performance-wise. See also Literature for a faster algorithm.

Everything began with an issue on swift-nio.

Status

  • support for base64 and base64url
  • faster than Foundation
  • decoding can ignore line breaks
  • encoding can insert line breaks

This package's encoding implementation is used in swift-nio's websocket implementation.

Performance

Super simple performance test to ensure speediness of this implementation. Encoding and decoding 1m times the base64 string:

AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==

macOS

MacBook Pro (15-inch, late 2016 - the first one with the butterfly keyboard). Quad Core 2.7 GHz Intel Core i7

Encoding Decoding
Foundation 2.21s 2.28s
swift-base64-kit 1.01s 1.06s
Speedup 2.18x 2.14x

linux

Whatevar runs GitHub Actions 😉

Encoding Decoding
Foundation 33.64s 3.49s
swift-base64-kit 1.07s 1.27s
Speedup 31.18x 2.74x

I have no idea why Foundation base64 encoding is so slow on linux. 🤷‍♂️

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:

Alternatives

As of today (2019-12-10), the author is aware of only one alternative that offers merely encoding.

You can’t perform that action at this time.