Fork me on GitHub
#clojurescript
<
2022-12-09
>
Ertugrul Cetin10:12:08

Hi guys, I have a re-frame + reagent app. It is an interesting case. I have a JavaScript array with a capacity of 500 (yes it has to be a JS object), it acts like a FIFO, and it gets populated between 50 - 200 items per second. This array keeps strings, basically, they are some messages. The thing is I need to render the messages in a message box or something, but I'm not sure it's doable in re-frame's subscription (using/returning JS object). It's a very performance-sensitive app, so I'm trying to avoid triggering GC (memory is my concern), that's why can't use an immutable vector. Any ideas on what would be the best way to render those messages in a performant way?e

p-himik10:12:21

Would be better suited for #C073DKH9P 200 times per second is nothing. You can safely use an immutable CLJS queue here. Either instead of the array or along with it.

p-himik10:12:07

Just a quick test:

ClojureScript 1.11.60
(let [value (str (random-uuid))]
  (time
    (reduce (fn [q _]
              (conj q value))
            #queue [] (range (* 200 500))))
  nil)
"Elapsed time: 26.789526 msecs"
nil

p-himik10:12:18

My bet is that just re-rendering all those 500 items (assuming it's 500 new items every time, 200 times per second) will dwarf that.

Ertugrul Cetin10:12:07

I'm not worried about execution time, the memory is my concern that triggers the GC (it will grow over time with immutable data structure)

p-himik10:12:21

It won't, if that queue is not growing. Should also be trivial to test. If you're seeing a different behavior, please provide an MRE so people here can test it and suggest what's wrong.

👍 1
p-himik11:12:11

Had a million items being put on and off the same queue (i.e. a mil conjs followed by a mil pops, in a loop) for half an hour. Memory usage barely registered.

Ertugrul Cetin12:12:58

Thanks for the experiment! I'll try also that