Why is `as.logical()` much faster than `logical(0)`?
as.logical() returns a logical(0):
as.logical()
#> logical(0)
but I’m surprised to see that logical(0) is much slower than as.logical(). Why is that? This is also the case for the other as.*() functions:
bench::mark(as.logical(), logical(0), iterations = 1e5)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#>
#> 1 as.logical() 48.1ns 76ns 8467061. 0B 0
#> 2 logical(0) 468ns 571ns 1541503. 0B 30.8
bench::mark(as.character(), character(0), iterations = 1e5)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#>
#> 1 as.character() 65ns 94.1ns 9734069. 0B 0
#> 2 character(0) 505ns 593ns 1550503. 0B 15.5
bench::mark(as.numeric(), numeric(0), iterations = 1e5)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#>
#> 1 as.numeric() 58.1ns 91ns 10218252. 0B 0
#> 2 numeric(0) 500ns 561ns 1622768. 0B 16.2
bench::mark(as.integer(), integer(0), iterations = 1e5)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#>
#> 1 as.integer() 64ns 92ns 9863536. 0B 0
#> 2 integer(0) 469ns 582ns 1435752. 0B 14.4