4 ms·
> JS can share data between threads, but must convert it all to string To be more precise, you can send data to web workers and worker threads by copying via t
by throwitaway1123 2y ago
> JS can share data between threads, but must convert it all to string
To be more precise, you can send data to web workers and worker threads by copying via the structured clone algorithm (unlike JSON this supports almost all data types), and you can also move certain transferable objects between threads which is a zero-copy (and therefore much faster) operation.
- moffkalast 2y agoAh yeah dataviews, but you still need to convert from json to those and that takes about as much overhead, plus they're much harder to deal with complexity-wise being annoying single type buffers and all. For any other language it would work better, but because JS mainly deals with data arriving from elsewhere it means it needs to be converted every single time instead of just maintaining a local copy for thread comms.
- throwitaway1123 2y ago> Ah yeah dataviews, but you still need to convert from json to those and that takes about as much overhead You don't necessarily need to have an intermediate JSON representation. Many of the built in APIs in Node and browsers return array buffers natively. For example: const buffer = await fetch('foo.wav').then(res => res.arrayBuffer()) new Worker('worker.js').postMessage(buffer, [buffer]) This completely transfers the buffer to the worker thread, after which it is detached (unusable from the sending side) [1][2]. [1] https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage#transfer https://developer.mozilla.org/en-US/docs/Web/API/Worker/post... [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/detached https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- moffkalast 2y agoHmm, well this I have to try sometime, thanks for the heads up :)