johnny-quesada-developer / easy-web-worker

This is a library to easily handle and create web-workers, if you are a web developer and never tried this... do not wait any longer! Workers are an amazing option to improve user experience and make your webpages even greater
MIT License
18 stars 1 forks source link

Performance issue in concurrency mode with maxWorkers=4, keepAlive=true, warmUpWorkers=true #11

Closed robertomonno77 closed 2 months ago

robertomonno77 commented 3 months ago

I noticed some performance issue configuring a pool of websocket in this way:

createEasyWebWorker( [ new Worker(new URL('path to my static file', import.meta.url)) ], { workerOptions: { type: 'module' }, maxWorkers: 4, keepAlive: true, warmUpWorkers: true, })

Basically here I would like to create a pool of 4 web-workers at the beginning of the configuration phase as the parameter warmUpWorkers suggests. Unfortunately from the tests I did it seems that the pool is not created immediately and this ends up with performance issue running the test application.

As a workaround my pseudo-code now looks like this one:

let ws = []; for (let idx = 0; idx < 4; ++idx) { ws.push(new Worker(new URL('path to my static file', import.meta.url))); } return createEasyWebWorker( ws, // that is an array of 4 elements { workerOptions: { type: 'module' }, maxWorkers: 4, keepAlive: true, warmUpWorkers: true, })

that seems to work correctly. I am not 100% sure this could be considered a bug (properly speaking), but something to share if anyone else needs...

johnny-quesada-developer commented 2 months ago

Hi @robertomonno77,

Thank you so much for reporting this, and you're absolutely right! The configuration wasn't being properly applied for concurrent mode, and the warmup process was being ignored. I’ve made the necessary changes.

The only case where the configuration will still be ignored is when working with an array of workers, which must be kept alive as there is no way to restore them once terminated.

To summarize, for your initial use case, everything should be working as expected now.

const worker = new EasyWebWorker(new URL('path to my static file', import.meta.url),
  {
    workerOptions: { type: 'module' },
    maxWorkers: 4,
    warmUpWorkers: true,
    keepAlive: true,
  }
);

The previous config will warm up 4 workers and keep them alive.