Closed larmarange closed 3 months ago
After the base R pipe was released, I recall reading an article (or tweet?) from hadley that they had re-written the magrittr pipe, and its efficiency is now similar to the base pipe. We can definitely begin to use the base pipe, but if we're going for straight efficiency gains, we could probably use {profvis} to identify places where we could get bigger gains?
library(stringr)
text <- "abcd"
fn_magrittr <- function(text) {
text %>% str_replace("a", "b") %>%
str_replace("b", "c") %>%
str_replace("c", "d")
}
fn_native <- function(text) {
text |> str_replace("a", "b") |>
str_replace("b", "c") |>
str_replace("c", "d")
}
bench::mark(
magrittr = fn_magrittr(text),
native = fn_native(text),
check = FALSE
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 magrittr 77.9µs 82.1µs 11575. 160.26KB 27.5
#> 2 native 77.7µs 80.4µs 12147. 1.55KB 27.2
Created on 2024-07-25 with reprex v2.1.0
(Obviously, I am no expert in reducing runtime, though! 😆 )
@ddsjoberg since gtsummary now requires R 4.2, maybe it could be a good timing for using the native pipe in broom.helpers as well?