How long does it take to call reactives and access reactiveValues?
library(shiny)
# Reactives with no changes (after first)
v <- reactiveValues(x = 0)
r0 <- reactive(1) # 0 levels of dependence
r1 <- reactive(v$x) # 1 level
r2 <- reactive(r1()) # 2 levels
# Reactives with change
vc <- reactiveValues(x = 0)
rc1 <- reactive(vc$x)
f_rc1 <- function() {
vc$x <- vc$x + 1
rc1()
}
rc2 <- reactive(rc1())
f_rc2 <- function() {
vc$x <- vc$x + 1
rc2()
}
library(microbenchmark)
isolate(microbenchmark(
v$x, # Use reactive value directly
r0(), # reactive with 0 levels
r1(), # reactive with 1 level
r2(), # reactive with 2 levels
vc$x <- vc$x + 1, # Change reactive value
f_rc1(), # Change reactive value, then call a dependent reactive (1 level)
f_rc2() # Change reactive value, then call a dependent reactive (2 levels)
))
## Unit: microseconds
## expr min lq median uq max neval
## v$x 228.4 239.3 246.0 254.9 1458 100
## r0() 379.7 399.9 410.8 434.1 2573 100
## r1() 381.5 398.2 411.9 424.5 3078 100
## r2() 376.7 405.3 418.9 432.4 2763 100
## vc$x <- vc$x + 1 640.1 686.4 1089.1 1593.1 2878 100
## f_rc1() 2899.1 3118.6 3471.7 3947.4 9271 100
## f_rc2() 4803.4 5158.6 5647.3 6112.7 44051 100
If the reactive has not been invalidated (there have been no changes to its ancestors in the reactive graph), it takes about 400us. If it has been invalidated, it takes about 2-3ms per level of reactivity.