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.0.13 30 Jan 2024
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   561   632   901.69   716.5   792  18295   100
##  base64enc  1543  1673  3511.38  1878.5  2144 158967   100
##    openssl 13906 14482 16639.74 14747.5 15063 151914   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   581   646.5  1090.61   781.5   912.0  30447   100
##  base64enc  1963  2124.0  2993.88  2524.5  2730.0  56145   100
##    openssl 21150 21916.0 24065.19 22272.0 23098.5 110697   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   204.872   223.1865   236.3153   234.748   242.899   401.83   100
##  base64enc  4817.009  4877.4120  5533.8939  4908.720  4998.849 43098.11   100
##    openssl 36829.237 37825.3265 38135.4735 37951.887 38312.852 42712.60   100