Benchmark

This small benchmark compares the performance of the base64 encoding/decoding in package base64url with the implementations in the packages base64enc and openssl.

Encoding of a single string

library(base64url)
library(base64enc)
library(openssl)
## Linking to: OpenSSL 3.5.5 27 Jan 2026
library(microbenchmark)

x = "plain text"
microbenchmark(
  base64url = base64_urlencode(x),
  base64enc = base64encode(charToRaw(x)),
  openssl = base64_encode(x)
)
## Unit: nanoseconds
##       expr  min      lq     mean median      uq    max neval
##  base64url  371   435.5   715.60    506   551.0  21462   100
##  base64enc 1132  1232.0  3214.01   1357  1532.5 169553   100
##    openssl 9754 10346.0 12774.60  10671 11166.5 197244   100

Decoding of a single string

x = "N0JBLlRaUTp1bi5KOW4xWStNWEJoLHRQaDZ3"
microbenchmark(
  base64url = base64_urldecode(x),
  base64enc = rawToChar(base64decode(x)),
  openssl = rawToChar(base64_decode(x))
)
## Unit: nanoseconds
##       expr   min      lq     mean median      uq    max neval
##  base64url   380   475.5   926.55    501   566.0  40710   100
##  base64enc  1543  1868.0  2832.50   2083  2303.5  75002   100
##    openssl 13710 14417.0 16482.08  14872 15338.0 142252   100

Encoding and decoding of character vectors

Here, the task has changed from encoding/decoding a single string to processing multiple strings stored inside a character vector. First, we create a small utility function which returns n random strings with a random number of characters (between 1 and 32) each.

rand = function(n, min = 1, max = 32) {
  chars = c(letters, LETTERS, as.character(0:9), c(".", ":", ",", "+", "-", "*", "/"))
  replicate(n, paste0(sample(chars, sample(min:max, 1), replace = TRUE), collapse = ""))
}
set.seed(1)
rand(10)
##  [1] "*MaHQn6Yu1gKKHRGIPLtBtRNR"       "MYPfxFnbSrv,mNVwCmvBVGSuE"      
##  [3] "qV:7YH"                          "aQ6zo5CxPV"                     
##  [5] "Mx0NIQaCvBK8T-YRW73WX"           "gtxY0pbV,R+sqHEITspNiXx"        
##  [7] "FMKlnpob,-"                      "qeOEJWOC:a040XDbJNK3AOo4"       
##  [9] "9fdI4y"                          "KB9tCP7,BElRzGd0xKon03XtbcLoB2/"

Only base64url is vectorized for string input, the alternative implementations need wrappers to process character vectors:

base64enc_encode = function(x) {
  vapply(x, function(x) base64encode(charToRaw(x)), NA_character_, USE.NAMES = FALSE)
}

openssl_encode = function(x) {
  vapply(x, function(x) base64_encode(x), NA_character_, USE.NAMES = FALSE)
}

base64enc_decode = function(x) {
  vapply(x, function(x) rawToChar(base64decode(x)), NA_character_, USE.NAMES = FALSE)
}

openssl_decode = function(x) {
  vapply(x, function(x) rawToChar(base64_decode(x)), NA_character_, USE.NAMES = FALSE)
}

The following benchmark measures the runtime to encode 1000 random strings and then decode them again:

set.seed(1)
x = rand(1000)
microbenchmark(
  base64url = base64_urldecode(base64_urlencode(x)),
  base64enc = base64enc_decode(base64enc_encode(x)),
  openssl = openssl_decode(openssl_encode(x))
)
## Unit: microseconds
##       expr       min        lq       mean    median        uq       max neval
##  base64url   126.869   145.691   222.7697   208.680   284.293   550.789   100
##  base64enc  4115.389  4250.290  4782.7923  4391.725  4521.828  8576.723   100
##    openssl 27146.293 29245.910 29972.6223 29557.638 29867.674 83608.013   100