2

I have thousands of small strings that I have to pass from a webworker back to the main page, each one is something like this:

"this string needs to be sent"

How would I be able to include it into an array buffer in order to increase the transfer speed? I understand how to use numbers with array buffers, but how do you use strings? I am looking for something like this:

var strings = ["str1","str2","str3",...]
for (var i = 0; i < strings.length; i++) {
    arraybuffer[i] = //Whatever operation works to add strings[i]
}
user16791137
  • 111
  • 6

1 Answers1

1

It's worth measuring and comparing performance of various techniques. The worker could use SharedArrayBuffer if supported in your target browsers (not exemplified below), otherwise Transferrable objects can be used with postMessage(). TextEncoder creates ArrayBuffers from strings.

Individual strings can be transferred as they are encoded:

const encoder = new TextEncoder()
strings.forEach(s => {
  const encoded = encoder.encode(s)
  postMessage(encoded, [encoded.buffer])
})

An array of strings could be transferred in batches:

const encoded = strings.map(s => encoder.encode(s))
postMessage(encoded, encoded.map(bytes => bytes.buffer))
anthumchris
  • 8,245
  • 2
  • 28
  • 53
  • Are there any special browser settings that have to be enabled for sharedarraybuffer? I tried using it on ms edge version 96, which is supposed to be supported, and it did not work. – user16791137 Dec 22 '21 at 19:54
  • [Requires cross-origin isolation](https://caniuse.com/sharedarraybuffer) • [SharedArrayBuffer updates](https://developer.chrome.com/blog/enabling-shared-array-buffer/) – anthumchris Dec 23 '21 at 18:22
  • See [this answer](https://stackoverflow.com/questions/70535752/enable-sharedarraybuffer-on-localhost/70536119#70536119) to quickly test `SharedArrayBuffer` – anthumchris Dec 30 '21 at 19:59