简体   繁体   English

有没有一种方法可以将对象数组作为可转移对象传递给网络工作者?

[英]Is there a way to pass an array of objects to a web worker as a transferable object?

I'm working on a CPU and memory intensive project for which some of the processing is sent to a web worker in order to not hang the browser while its number crunching. 我正在处理一个CPU和内存密集型项目,该项目的某些处理已发送给Web Worker,以便在处理数字时不会挂起浏览器。 The problem I am facing is that I need to send to the web worker instance a few multidimensional arrays, but upon profiling the app I realized it was cloning the arrays, so I'l trying to see if I pass them as transferable objects. 我面临的问题是我需要向Web Worker实例发送一些多维数组,但是在对应用程序进行性能分析时,我意识到它正在克隆数组,因此,我将尝试查看是否将它们作为可传递对象传递。

For simplicity assume that the arrays I'm trying to pass are: 为了简单起见,假设我要传递的数组是:

var myArray1 = [{a: 0, b: "0"}, {a: 1, b: "0"}]; var myArray1 = [{a:0,b:“ 0”},{a:1,b:“ 0”}];

var myArray2 = [{c: 0, d: "0"}, {c: 1, d: "0"}]; var myArray2 = [{c:0,d:“ 0”},{c:1,d:“ 0”}]];

Is there a way to pass these as transferable objects to a web worker instance? 有没有办法将这些作为可传递对象传递给Web Worker实例?

Not directly but ArrayBuffers and SharedArrayBuffers can be transferred to web workers and if your data is as uniform as in your example then it would be possible to store that data in an array buffer rather than an array of objects. 可以不直接将ArrayBuffersSharedArrayBuffers传递给Web Worker,并且如果您的数据与示例中的相同,则可以将数据存储在数组缓冲区而不是对象数组中。

Instead of 代替

const arr = [{ a: 0, b: '0' }];

you might store the data as 您可以将数据存储为

const ab = new ArrayBuffer(2 * 4);
const dv = new DataView(ab);
dv.setFloat32(0, 0);
dv.setUint32(4, '0'.charCodeAt(0));

And then read it back using a data view in the worker, as well. 然后,也使用worker中的数据视图将其读回。 This will let you transfer the data to the worker using a transferable item. 这样您就可以使用可转让项目将数据转让给工人。 Of course this all depends on your data and how it's structured. 当然,这一切都取决于您的数据及其结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 JavaScript - postMessage() 中的 web worker 传递可转移对象数组,即缓冲区以及不可转移的 object,即 json obj? - How to pass array of transferable objects i.e. buffer along with non transferable object i.e. json obj via web worker in JavaScript - postMessage()? 使用 Web Worker 中的可转移对象 - Using transferable objects from a Web Worker Memory 泄漏:JavaScript - 使用可转移的 object 将数组缓冲区传递给 Web Worker,GC 不会启动 - Memory Leak: JavaScript - passing array buffer to Web Worker using transferable object, GC doesn't kick in 如何将数组中的多个数据作为可传递对象传递 - how to pass multiple data in array as a transferable object Web Workers - JSON的可转换对象 - Web Workers - Transferable Objects for JSON 这是一种在 Web 工作线程之间传递完整对象的方法吗? - Is this a way to pass full objects between web worker threads? Javascript:如何为 JSON 数组创建可传输的 Object - Javascript: How to create Transferable Object for JSON array Echartjs 中的 web worker 实现问题“无法在 'Worker' 上执行 'postMessage':索引 0 处的值没有可转移类型。” - Issues with web worker Implementation in Echartjs "Failed to execute 'postMessage' on 'Worker': Value at index 0 does not have a transferable type." 如何将全局数组传递给Web Worker并返回它? - How to pass a global array to web worker and return it? Javascript:如何将数组缓冲区与视图一起使用以发送可转移对象 - Javascript: How to use an array buffer with views to send transferable Objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM