有没有办法在不使用 Javascript 中的 new 关键字的情况下从 ArrayBuffer 创建 TypedArray?

Is there a way to create a TypedArray from an ArrayBuffer without using new keyword in Javascript?

我正在使用 transferables 在主线程和 worker 之间进行通信。我意识到通信创建了 GC activity 因为在收到缓冲区后,我正在使用 new 关键字将缓冲区转换为类型化数组:

var ary = new Float32Array(buffer);

有没有办法在不创建 GC 的情况下重新使用 TypedArray 或获取缓冲区视图 activity?

我认为没有,没有。 ArrayBuffer 几乎是一个没有类型化数组或 DataView 的黑盒子来查看它,并且您不能更改现有类型化数组或 DataView.[=18= 上的缓冲区]

在支持它的平台上,您可以创建 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 ).

但遗憾的是,大多数浏览器都禁用了 SharedArrayBuffer 以响应 Spectre,而我最后检查的只是 Chrome 重新启用了它(在启用了站点隔离功能的平台上)。