Skip to content

map* functions are slow when .f is a string #820

Description

@wch

For example, map_chr() is 10x slower than vapply():

library(purrr)
x <- lapply(1:10000, function(name) list(a = "A"))

microbenchmark::microbenchmark(
  map_chr(x, 'a'),
  vapply(x, `[[`, 'a', FUN.VALUE = "")
)
#> Unit: milliseconds
#>                                  expr       min        lq      mean    median        uq      max neval
#>                       map_chr(x, "a") 28.079812 30.615371 31.434516 31.281444 31.856803 42.96536   100
#>  vapply(x, `[[`, "a", FUN.VALUE = "")  3.080842  3.346405  3.826315  3.640201  3.894849 13.50321   100

A bit slower would be OK, but this is way slower than the base alternative. For my particular use case, I was surprised to find that a single map_chr call was the most expensive part of my code (taking about 78% of the time); after switching to vapply, it was much faster.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions