Given a typed array like Uint8Array it seems there are two approaches to transfer them via a worker.
Option 1
Send the buffer directly and cast it on the receiving side:
Sender: postMessage({fooBuffer: foo.buffer}, [foo.buffer])
Receiver: const bar = new Uint8Array(msg.data.fooBuffer)
Option 2
Send the TypedArray and only transfer its buffer:
Sender: postMessage({foo: foo}, [foo.buffer])
Receiver: use foo as-is.
It seems to me that option 2 is preferable since the receiver doesn't need to know about the type of data, and its less code - but I keep coming across examples only in the style of option 1.
More to the point, in my current code, only option 2 works. I've confirmed that the data is transferred since only after sending, foo[0] becomes undefined.
Is using option 2 okay, even though it's not the norm for sample code I'm seeing?