rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

Converting `tags` to `character` unusable slow when scaled #326

Open markschat opened 2 years ago

markschat commented 2 years ago

Converting htmltools::tags to character is very slow compared to writing plain html. TagLists make things even worse:

library(dplyr)
library(htmltools)
library(microbenchmark)

raw_html <- function(){
  paste0(
    "<button>btn</button>",
    "<a>a</a>",
    "<span>x</span>"
  )
}

tag_to_char <- function(){
  as.character(tags$button("btn"))
}

tag_list_to_char <- function(){
  as.character(tagList(
    tags$button("btn"), 
    tags$a("a"), 
    tags$span("x")
  ))
}

res <- microbenchmark(raw_html(), tag_to_char(), tag_list_to_char())
print(res)
plot(res)

My benchmark

Unit: microseconds
               expr      min        lq       mean    median        uq      max neval
         raw_html()    2.242    3.5895   19.70312    5.5445    5.8605 1451.932   100
      tag_to_char()  451.579  465.9735  505.45220  474.9260  494.0190 2081.677   100
 tag_list_to_char() 1140.821 1158.0015 1309.62663 1178.0970 1285.7785 5321.876   100

I don´t know if that is a fair comparison but in my particular use case I have a large table in a shiny app. I compute html dropdown-inputs for each row. My first attempt using htmltools::tags and converting to character took several seconds to mutate the table (-> app unusable) while pasting raw html computed in the blink of an eye.

I would love to use htmltools here as pasting raw html really hurts my heart - thank´s for this amazing package btw 🙌