2

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?

1
  • i doubt there is a way Commented Mar 24, 2019 at 18:02

1 Answer 1

1

I don't think there is, no. ArrayBuffer is pretty much a black box without a typed array or DataView to look into it, and you can't change the buffer on an existing typed array or DataView.

On platforms that support it, you can create a SharedArrayBuffer that 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 via postMessage or Atomics (more about that in this question's answer).

But sadly, most browsers disabled SharedArrayBuffer in response to Spectre, and the last I checked only Chrome has re-enabled it (on platforms where its site isolation feature is enabled).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.