When looking for execution speed in an application, it's tempting to reach for a java.util.ArrayList
both when constructing maps and vectors. However there are some pitfalls in doing so. It is not obvious that for eg vectors, one should use LazilyPersistentVector/createOwning
over eg PersistentVector/adopt
, the former working correctly, whereas the only works for vectors smaller than 32.
Likewise for maps, where one could choose to create a PersistentArrayMap
if there are less than 9 entries in the map, but should use a PersistentHashMap
if there are 9 or more entries.
I don't want to prescribe solutions here, but a couple of fns in clojure.core
that took arrays as params and returned the appropriate data structure:
(array->vector arr) ;; does what LazilyPersistentVector/createOwning does
(array->map arr) ;; returns a PAM or PHM depending on size