Skip to content

Conversation

@m-muecke
Copy link
Contributor

compact1 <- function(x) Filter(length, x)
compact2 <- function(x) x[as.logical(lengths(x))]
compact3 <- function(x) x[lengths(x) > 0L]

x <- list(1:100, NULL, NULL, NULL, 1:100)
bench::mark(
  compact1(x),
  compact2(x),
  compact3(x),
  iterations = 100
)
#> # A tibble: 3 × 6
#>   expression       min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>  <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 compact1(x)   2.91µs   3.28µs    13534.        0B        0
#> 2 compact2(x) 491.97ns 574.04ns   115615.        0B        0
#> 3 compact3(x) 492.03ns 574.04ns   138659.        0B        0

Created on 2025-01-23 with reprex v2.1.1

@lionel- lionel- merged commit b53ac84 into r-lib:main Jan 24, 2025
12 of 13 checks passed
@lionel-
Copy link
Member

lionel- commented Jan 24, 2025

Thanks!

@m-muecke m-muecke deleted the compact branch January 25, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants