What Nate told me about Array building

Nate Faubion on Array building:

In my experience, using unsafe ST is not necessary … I switched to building a reversed List, then using Array.fromUnfoldable and Array.reverse. Both of those array operations are extremely cheap. Array.fromUnfoldable specifically builds an array with mutation and does not need to copy the final result. Array.reverse is much cheaper than List.reverse.

ST still has to allocate effect closures and binds at each step, which will be equivalent to allocating Cons cells.

I’ve tried this advice in a few places and it’s always easier to program and slightly faster at runtime. Instead of building an Array with Data.Array.ST.push, build up a reversed rlist :: List with cons and then

Array.reverse $ Array.fromFoldable rlist

Also in my experience Nate’s method is only slightly slower than doing unsafe FFI calls to JavaScript Array.push().