1

What is the difference in terms of efficiency and under-the-hood details

between (apply conj [0][1]) and (into [0][1])?

Using the time function it seems that apply conj is faster, but is there a price? and what is it?

5
  • 2
    could you please provide your benchmarks? It could happen, that the time difference is irrelevant, since you're benchmarking on some short collections for example. For long vectors into should be significantly faster, since it uses transient sequences, according to it's source. For the short ones conj could be a bit faster since it doesn't use any additional machinery, but for clojure.lang.RT/conj.
    – leetwinski
    Commented Dec 27, 2023 at 23:31
  • @leetwinski I timed the functions again and this time I got opposite results, leaving me a little puzzled. What is the actual difference between the two methods? What is preferable when? And you seem to be right at least about long vectors, into is about twice as fast joining two vectors of a size 100 each than using apply conj. So that leaves the question only regarding small vectors, which is in fact preferable? Commented Dec 28, 2023 at 8:35
  • 1
    time is not suitable for benchmarking small things. Check out github.com/hugoduncan/criterium
    – cfrick
    Commented Dec 28, 2023 at 10:02
  • One question you could ask yourself: "If (apply conj xs ys) were better than (into xs ys), why would anyone ever use into for anything? Why would into even be defined?"
    – amalloy
    Commented Dec 28, 2023 at 10:31
  • @amalloy into could be better simply because it's one word. Since the implementation of into is different, that aroused the question "why is into not implemented with apply conj"?. Commented Jan 5 at 10:58

1 Answer 1

-1

The into function is generally preferable to apply conj in Clojure due to the following reasons:

  • Performance: into is usually faster than apply conj. The into function is implemented in a way that allows for optimized, efficient concatenation of collections. On the other hand, apply conj involves using conj repeatedly with variadic arguments, which can be less performant.

  • Simplicity & Readability: into provides a simpler and more concise way to concatenate collections. It takes care of the concatenation internally without requiring explicit iteration or multiple function calls. Using into makes the code more readable and expressive. It clearly communicates the intention of concatenating or merging collections, improving the overall code clarity.

  • Flexibility: into supports merging of collections, not just concatenation. It can merge collections of different types or with conflicting keys, providing more flexibility in combining data structures.

That being said, there may be specific cases where apply conj is more suitable, such as when you need to dynamically pass in collection elements as variadic arguments. However, for most general cases, the into function is the preferred way to concatenate or merge collections in Clojure.

Not the answer you're looking for? Browse other questions tagged or ask your own question.