I have an ArrayBuffer with content that needs to be converted to a hex-string, that should be sent in a JSON message. The data can be up to 2 GB. For this I would like to have a string that works as a dynamic array, so I efficiently could push_back individual characters. In C++, we have std::string (or std::vector), I think Java has a StringBuilder. What is the way of doing it in JavaScript?
I tried += but that starts to take minutes after reaching 128 MB. I guess this is because it results in an O(n^2) algorithm.
Note: I also need to do the inverse operation.
.join()it, but 2 gigabytes (well, 8 once it's turned into a hex string, which really means 10 because the buffer is still there) is going to be pretty taxing on the machine.+=normally is optimised by JS engines into a constant average operation, but I guess this might break down when working with humongous strings. As Pointy suggested, do not build the message as a single string value but rather write it into a stream.