elm-cardano/base64/1.0.1

elm-cardano/base64

Fast Base64 encoding and decoding for Elm Bytes.

API

import Base64

Base64.fromBytes bytes         -- Bytes -> String
Base64.toBytes "Zm9v"          -- String -> Maybe Bytes
Base64.toBytesUnsafe "Zm9v"    -- String -> Bytes (no validation)

See the module docs for the full API.

Install

elm install elm-cardano/base64

Performance

Performance is a first-class concern for this package. Every function has been benchmarked and optimized, with multiple implementation strategies tested and compared. Here are the key techniques:

  • Batched decoding: fromBytes reads 60 bytes per outer loop iteration (via Decode.map5 over five 12-byte Decode.map3 blocks), minimizing loop overhead. Each 12-byte block unpacks three Int32 words into four 24-bit groups and emits 16 base64 characters.
  • ConsString-aware accumulation: String concatenation with (++) is O(1) in V8 (creates a rope node), which we exploit throughout. This beats List String accumulators with String.concat by ~11%.
  • Fused validation: The safe decoder (toBytes) validates input characters inline during the decode fold, using a bad accumulator that OR-collects sentinel values. One pass, no separate validation walk — within ~4% of the unsafe path.
  • String.foldl-based decoding: The decoder operates on the input string directly via String.foldl, building output through Encode.Encoder lists, avoiding Bytes.Decode-side accumulation entirely.

As a reference point, here is how we compare to danfishgold/base64-bytes (the most popular Elm Base64 package):

FunctionSizeelm-cardano/base64danfishgold/base64-bytesSpeedup
fromBytes0-32 B18,144 ns20,599 ns14%
fromBytes1024 B19,299 ns24,704 ns28%
fromBytes10 kB190,233 ns242,915 ns28%
toBytes16 B579 ns953 ns65%
toBytes1024 B27,105 ns50,688 ns87%

See bench/ for the full benchmarking setup and methodology. See bench/REPORT.md for the history of attempted optimizations.

Development

pnpm install
pnpm test           # run tests
pnpm review         # elm-review
pnpm format:check   # check formatting
pnpm bench:build    # build benchmarks
pnpm bench:test     # run benchmark correctness checks