I am using transferables to communicate between the main thread and the worker. I realized the communcation creates GC activity because after receiving the buffer, I'm converting the buffer to a typed array using the new keyword:
var ary = new Float32Array(buffer);
Is there a way to re-use a TypedArray or getting a view of a buffer without creating GC activity?
I don't think there is, no.
ArrayBufferis pretty much a black box without a typed array orDataViewto look into it, and you can't change the buffer on an existing typed array orDataView.On platforms that support it, you can create a
SharedArrayBufferthat both the main and worker thread have access to, which wouldn't have the GC problem since each side would reuse its wrapper array. Just make sure you gatepost access to it viapostMessageorAtomics(more about that in this question's answer).But sadly, most browsers disabled
SharedArrayBufferin response to Spectre, and the last I checked only Chrome has re-enabled it (on platforms where its site isolation feature is enabled).