Reputation: 67722
I am trying to use Web Workers, and I noticed that to improve performance you can use "transferable" objects in postMessage
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
In Deno, transferable object means ArrayBuffer, but how to transform an array variable to ArrayBuffer ?
my use case is sending array of around 10k elements to the web worker, so I'd like to not copy it if possible.
Upvotes: 0
Views: 87
Reputation: 9090
An array with strings could technically be transferred by creating a binary array from a serialized string:
// sender
arr = [1, 2, 'three']
bytes = new TextEncoder().encode(arr.join('|'))
postMessage(bytes, [bytes.buffer])
// receiver
onmessage = event => new TextDecoder().decode(event.data).split('|')
However, standard serialization should outperform that conversion/transfer:
postMessage(arr) // sender
onmessage = event => event.data // receiver
Upvotes: -2