I’m in the process of building a binary encoder/decoder. While benchmarking it, I had to generate binaries to feed to it. I noticed that it’s rather slow to do so.
String.duplicate(<<224>>, 768000) takes ~4500 microseconds on my computer, I believe it’s the best optimized for this use case.
The version you have is doing a lot more, you’ve got a full blown comprehension that has to iterate through a range and collect results, and then finally stitch those results into a binary. String.duplicate calls :binary.copy which is a BIF.
Notably though, constants like this would be more idiomatically generated at compile time and then simply referenced at runtime:
Wow, that is a huge difference! Not as close as Node.js, but this is a much better improvement. I swear, I have been looking at the manual page for :binary the whole time when working on the encoder, and yet I missed :binary.copy/2 completely. Thanks!
My encoder is still very slow compared to the Node.js version, but I can see that there are ways to improve binary handling.