gpt4 book ai didi

javascript - 将 JS 对象转换为 ArrayBuffer 以传输到/从 web worker 等于瓶颈

转载 作者:数据小太阳 更新时间:2023-10-29 05:39:30 25 4
gpt4 key购买 nike

我有一个 web worker,它不断计算大量数据,当完成一轮时,它是一个 js 对象,然后我将其解析为 ArrayBuffer,然后将其发送到主线程。

不能做太多的计算,ArrayBuffer 的传输很快。但是这个对象的解析却减慢了这个过程。由于对象本身包含更多对象的数组。

在 Firefox 中我收到以下警告:

A script on this page may be busy, or it may have stopped responding.
You can stop the script now, open the script in the debugger,
or let the script continue.

Script: http://localhost/js/util/DataViewSerializer.js:435

第 435 行指的是我序列化数组的函数。该行是声明 for 循环的地方。

DataViewSerializer.prototype.setArray = function (array, serializer) {
var i,
l = JSUtil.hasValue(array) ? array.length : 0;

this.setUint32(l);

console.log(array, serializer);
for (i = 0; i < l; i += 1) {
if (serializer !== undefined) {
serializer.serializeTo(array[i], this);
} else {
array[i].serializeTo(this);
}
}
};

我正在阅读 Transferable Object在 web worker 和主线程之间。因为发送 ArrayBuffers 似乎是唯一的方法。

所以我的问题是是否有更快的方法将 js 对象转换为 ArrayBuffer?或者任何其他加快速度的建议?

最佳答案

这种性质的问题已经在 web workers 中被问过 3 次了。这是我用很长的答案回答的两个问题:

鉴于我已经写了很多文字,我将重复主题:

Do not use custom serialization in favor of Structured clone algorithm unless your data aren't supported by Structured clone algorithm!

也就是调用:

worker.postMessage({name: "MY_MESSAGE", data:myArray});

关于javascript - 将 JS 对象转换为 ArrayBuffer 以传输到/从 web worker 等于瓶颈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39464083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com