-
Notifications
You must be signed in to change notification settings - Fork 138
Description
Currently, we use utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE)) to get the string representation of each object in global environment. The overhead will become more significant as there are more symbols in it.
Suppose the global environment contains as many symbols as base environment (more than 1200 visible symbols), the overhead of getting object metadata will be more than 300ms, i.e., on each user input, there will be an extra 300ms to wait for the metadata of objects to be gathered.
To reduce the overhead, we could avoid using capture.output and str and instead create a simple string that contains simple information such as class, type, length, dim to represent the object.
system.time(objs <- eapply(baseenv(), function(obj) {
utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE))
}))
#> user system elapsed
#> 0.335 0.004 0.339
system.time(objs <- eapply(baseenv(), function(obj) {
list(
type = typeof(obj),
length = length(obj),
class = class(obj)
)
}))
#> user system elapsed
#> 0.013 0.001 0.013If the time of writing files are included,
system.time({
objs <- eapply(baseenv(), function(obj) {
list(
type = typeof(obj),
length = length(obj),
class = class(obj),
str = trimws(utils::capture.output(utils::str(obj, max.level = 0, give.attr = FALSE))),
names = names(obj)
)
})
jsonlite::write_json(objs, "test.json", auto_unbox = TRUE, pretty = FALSE)
})
#> user system elapsed
#> 0.557 0.011 0.570
system.time({
objs <- eapply(baseenv(), function(obj) {
list(
type = typeof(obj),
length = length(obj),
class = class(obj),
names = names(obj)
)
})
jsonlite::write_json(objs, "test.json", auto_unbox = TRUE, pretty = FALSE)
})
#> user system elapsed
#> 0.113 0.000 0.113