josdejong / workerpool

Offload tasks to a pool of workers on node.js and in the browser
Apache License 2.0
2.04k stars 148 forks source link

Question re: data serialisation #444

Closed TimUnderhay closed 4 months ago

TimUnderhay commented 4 months ago

Hello M. de Jong,

A (hopefully) quick question for you. The documentation states:

"Note that both function and arguments must be static and stringifiable, as they need to be sent to the worker in a serialized form. In case of large functions or function arguments, the overhead of sending the data to the worker can be significant."

This is written in the context of offloading functions dynamically. Does that mean that serialisation is a concern only when using dynamic functions, or is it is a universal concern with workerpool? Or to put it another way, it would be helpful to know in exactly which cases data gets (de)serialised. I.e are arguments passed in exec() (using function name arg) / proxy(), and their return values serialised?

Cheers!

josdejong commented 4 months ago

The data is always serialized when sent to the worker. This serialization is not specific to workerpool, but is the case for web workers and worker threads in general. If you have anything other than "plain data", it may be changed when sending it to the worker. Like some class instance, or a function containing references or so.