This is a crude proposal of a direction I think we can take worker_threads forward in, and I thought I should post it to get community feedback.
I do understand that we are trying to align worker_threads with WebWorkers, but considering the fact that server requirements are different from client, and to take advantage of the fact that workers have their own event loop, I think we should support doing the following:
let aVeryLargeArray = [1, 2, 3, 4, 5 .... 100000000];
const workerThreads = require('worker_threads');
const messageChannel = workerThreads.start((channel, input) => {
const resultArray = input.map((item) => item * 2);
channel.port1.postMessage({ resultArray });
}. aVeryLargeArray); // just like setTimeout with the channel reference as an input to the function
messageChannel.port1.on("message", ({resultArray}) => {
console.log("The result array obtained is ": resultArray);
channel.port1.close();
});
messageChannel.port1.start();
The main benefit of this which I see that it would make it easier to manipulate very large data sets on the server, synchronously if needed, without starving the event loop of the main thread(kind of using the workers like how fs uses the UV Thread Pool)
/cc @addaleax @nodejs/workers
EDIT:
Another option we could use would be blocks
with the existence of https://github.com/domenic/proposal-blocks I think it would be best if node didn't move on its own
@devsnek Yes that's the proposal I meant by the "blocks" in the title. I would prefer it too if we could use that, but it depends on tc39. By any change, do you know what stage it is at?
@AyushG3112 its at stage 0, i believe @domenic is presenting it during the july meeting. (please correct me if i'm wrong)
It takes a string at the moment with eval:true IIRC
I鈥檒l close this for now, as I don鈥檛 see adding support for functions as something we鈥檇 do in the near future.
If somebody disagrees, feel free to (ask to) reopen, and/or open a PR. :) I鈥檇 personally wait for bl枚cks to happen, if that doesn鈥檛 stall out.
Most helpful comment
with the existence of https://github.com/domenic/proposal-blocks I think it would be best if node didn't move on its own