related to this question: Javascript worker.postMessage shrinks empty array
When an array that contains undefined
elements is given to Worker.prototype.postMessage
the array that arrives has been messed with. It's length remains the same, but it is no longer possible to iterate using .map
, .reduce
, for...in
etc.
The below code outputs 2 0
. Is this a bug, or is it somehow expected behavior? The same happens if you pass an array with partial values [undefined, 2]
or [1, undefined]
for example.
var response = "self.onmessage=function(msg){console.log(msg.data.length, msg.data.reduce(function(i){return ++i;},0))}";
var blob = new Blob([response], {type: 'application/javascript'});
var worker = new Worker(URL.createObjectURL(blob))
worker.postMessage([undefined, undefined])