r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
403 stars 56 forks source link

Improve test for Lists vs. environments, and $ vs. [[ #284

Open MLopez-Ibanez opened 4 months ago

MLopez-Ibanez commented 4 months ago

The example here: https://github.com/r-lib/R6/blob/507867875fdeaffbe7f7038291256b798f6bb042/vignettes/Performance.Rmd#L712-L723

is too trivial to show the actual differences. A better example would be

lst <- as.list(sample(letters))
names(lst) <- unlist(lst)
env <- new.env()
for (x in lst) assign(x, x, envir=env)
mb_summary(microbenchmark(
  lst = lst$x,
  env = env$x,
  lst[['x']],
  env[['x']]
))

which gives in my computer:

         name median
1        lst 0.6345
2        env 0.2850
3 lst[["x"]] 0.4085
4 env[["x"]] 0.1960