Javascript worker.postMessage缩小空数组 [英] Javascript worker.postMessage shrinks empty array

查看:95
本文介绍了Javascript worker.postMessage缩小空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个空数组传递给一个worker,它应该填充数组并返回它 - 我知道还有其他方法可以做到这一点,但我更感兴趣的是为什么它不工作,而不是让它工作。



主要代码:

  var arr = new Array(4 )
console.log(arr.length)//输出4
var worker = new Worker(whatever.js)
worker.postMessage(arr)

工人代码

  self .onmessage = function(msg){
console.log(msg.data.length)//输出0
}

如果我传入一个填充数组,它就可以工作。如果我甚至将数组的单个条目设置为一个值,它就可以工作。



为什么 postMessage 函数折叠非零长度,但空阵列,有没有办法避免这种情况? (除了手动赋值)


我使用的是Windows 7和Chrome 51

结构化克隆算法,所以你的工作者没有得到你创建的确切数组,这是一个线程不安全的操作,因为数组是一个引用类型。这是MDN文档不完整的情况,一般来说,除了少数例外,结构化克隆算法等同于 JSON.parse(JSON.stringify(x)),但是这个是一个例外,如 JSON.parse(JSON.stringify(new Array(4)))产生 [null,null,null,null] code>至少在chrome和ff中。

这里怪异的行为可能部分是因为确实没有任何意义来创建具有空槽的数组那甚至没有任何迹象,例如 new Array(4).forEach(i => console.log('foo')); 不做任何事情。因此,结构化克隆算法会生成一个空数组。

注意,结构化克隆算法不是JavaScript规范的一部分,而是部分的 HTML 5规范,据我所知不会给出了很多细节,所以我不完全确定它是如何工作的,但似乎集中在诸如fileData和Blobs之类的东西上。请注意,克隆arraybuffer nofollow> Ecmascript规范。所有这些都是有道理的,工作人员由于相同的算法而在通信开销方面有巨大的性能损失(这就是为什么共享内存结构已经被提出)。所以你想要在他们中做的计算必须足够密集以超过启动和通信处罚。因此,沟通渠道更侧重于那些底层(本质上是二进制的)数据结构是有道理的。


I am passing an empty array to a worker, which should populate the array and return it - I know there are other ways of doing this, but I'm more interested in why it isnt working, than getting it to work.

The main code:

var arr = new Array(4)
console.log(arr.length)//outputs 4
var worker = new Worker("whatever.js")
worker.postMessage(arr)

The worker code

self.onmessage = function(msg){
    console.log(msg.data.length)//outputs 0
}

If I pass in a populated array, it works. If I even set a single entry of the array to a value, it works.

Why does the postMessage function collapse non-zero-length, but empty arrays, is there a way of avoiding this? (apart from manually assigning a value) .

I'm using Windows 7 and Chrome 51

解决方案

Data is copied to workers via the structured cloning algorithm, so your worker is not getting the exact array you created, that would be a thread-unsafe operation as arrays are a reference type. This is a case where the MDN docs are incomplete, generally speaking with a few exceptions the structured cloning algoritm is equivalent to JSON.parse(JSON.stringify(x)) but this is an exception as JSON.parse(JSON.stringify(new Array(4))) yields [null, null, null, null] at least in chrome and ff.

The weird behavior here is probably in part because there really isn't any point to creating arrays with empty slots like that, there aren't even any indicies, e.g. new Array(4).forEach(i => console.log('foo')); doesn't do anything. So the structured cloning algorithm generates an empty array.

Note that the structured cloning algorithm is not part of the JavaScript specification, it is rather part of the HTML 5 spec and as far as I can tell doesn't give a lot of details, so I'm not entirely sure how it works, but seems to be focused on things like fileData and Blobs. Note that cloning an arraybuffer is part of the Ecmascript spec. All of that makes some sense, workers have a huge performance penalty for the communication overhead (which is why shared-memory constructs have been proposed) in no small part due to that same algorithm. So the computation you want to do in them has to be intensive enough to outweigh the startup and communication penalties. So it makes sense that the communication channel is more focused on those low-level (essentially binary in the blob case) data constructs.

这篇关于Javascript worker.postMessage缩小空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆