Skip to content

Instantly share code, notes, and snippets.

@PietrH
Created February 26, 2026 15:22
Show Gist options
  • Select an option

  • Save PietrH/9b73f6bde644d4f39a93fdd7235f7159 to your computer and use it in GitHub Desktop.

Select an option

Save PietrH/9b73f6bde644d4f39a93fdd7235f7159 to your computer and use it in GitHub Desktop.
What is the fastest way to read the header of a delim?
readr_lazy_colnames <- function(path, delim = "|") {
readr::read_delim(
path,
delim = delim,
lazy = TRUE,
progress = FALSE,
show_col_types = FALSE
) |>
colnames()
}
arrow_colnames <- function(path, delim = "|") {
arrow::open_delim_dataset(path, delim = delim) |>
colnames()
}
arrow_schema_colnames <- function(path, delim = "|") {
arrow::open_delim_dataset(path, delim = delim)$schema |>
names()
}
arrow_nodf_colnames <- function(path, delim = "|") {
arrow::read_delim_arrow(path, delim = delim, as_data_frame = FALSE) |>
colnames()
}
readr_nmax_colnames <- function(path, delim = "|") {
readr::read_delim(
path,
delim = delim,
n_max = 0,
progress = FALSE,
show_col_types = FALSE
) |>
colnames()
}
readlines_colnames <- function(path, delim = "|") {
strsplit(readLines(path, n = 1), split = delim, fixed = TRUE)[[1]]
}
vroom_colnames <- function(path, delim = "|") {
vroom::vroom(
path,
delim = delim,
n_max = 0 ,
show_col_types = FALSE,
progress = FALSE
) |>
colnames()
}
dt_colnames <- function(path, delim = "|") {
data.table::fread(
nrow = 0,
sep = delim,
header = TRUE,
cmd = sprintf("head -n 1 %s", path)
) |>
colnames()
}
# Big csv file (pipe separated in my case, not provided!
big_csv <- "~/Downloads/wcvp_taxon.csv"
bench::mark(
readr_lazy_colnames(big_csv),
readr_nmax_colnames(big_csv),
arrow_colnames(big_csv),
arrow_schema_colnames(big_csv),
arrow_nodf_colnames(big_csv),
readlines_colnames(big_csv),
vroom_colnames(big_csv),
dt_colnames(big_csv)
)
@PietrH
Copy link
Author

PietrH commented Feb 26, 2026

expression             min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time result
  <bch:expr>        <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm> <list>
1 readr_lazy_colna…  208.3ms 214.54ms      4.58    6.01MB     0        3     0      655ms <chr> 
2 readr_nmax_colna… 531.01µs 600.75µs   1505.      6.31KB     9.13   659     4      438ms <chr> 
3 arrow_colnames(b…   5.93ms   7.56ms    123.     22.92MB     2.05    60     1      488ms <chr> 
4 arrow_schema_col…   6.23ms   7.36ms    136.     11.66KB     4.23    64     2      472ms <chr> 
5 arrow_nodf_colna… 245.64ms 248.67ms      4.01    3.79MB     0        3     0      749ms <chr> 
6 readlines_colnam…  11.73µs  12.54µs  64088.        520B     6.41  9999     1      156ms <chr> 
7 vroom_colnames(b… 506.75µs 564.36µs   1581.    466.03KB     9.11   694     4      439ms <chr> 
8 dt_colnames(big_…  868.4µs 954.75µs    987.      1.65MB     2.04   483     1      489ms <chr> 

@PietrH
Copy link
Author

PietrH commented Feb 26, 2026

Using readlines is by far the fastest way to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment