25 days ago

Does having an idle web worker result in significant overhead?

Can anyone explain the purpose of worker.terminate() to me? When I'm done with a web worker, what's the advantage of killing it instead of leaving it running for next time? (Creating new web workers can take up to 200ms based on my recent tests, so keeping them alive is potentially a big time-saver.) If I never need the worker again, is there any harm in leaving it running?

Presumably, each web worker has its own event loop, so there may be a slight overhead from that, but I don't think it would cause significant lag provided the workers don't perform any calculations during their idle time.

Based on my own testing, having more than 2500 web workers can cause the tab to crash, but as long as I stay below that limit, they seem not to affect page performance after the initial pause during their initialization. Moreover, I don't think having 2000 workers in idle mode in the background would cause any delay in CPU-intensive tasks or conflict with any aspect of the web page or browser.

So, my question is: Are there any performance reasons for invoking worker.terminate()? If so, what are they?

24 days ago
Verified Answer
The Worker.terminate() method immediately terminates the worker without giving it any chance to finish what it's doing. This does not offer the worker an opportunity to finish its operations; it is simply stopped at once. The close() method is visible inside the worker's scope. The terminate() method is a part of the worker object's interface and can be called "from the outside". There are no performance reasons for invoking Worker.terminate(). It is used when you want to stop the worker immediately without giving it any chance to finish what it's doing. If you don't need the worker again, there is no harm in leaving it running. I hope this helps! Let me know if you have any other questions.


javascript - Do I need to terminate / close / delete a web worker when ...Is it possible to terminate a running web worker?JavaScript Web Worker - close() vs terminate() - Stack OverflowWorker: terminate() method - Web APIs | MDN - Mozilla Developer7 Things You Need To Know About Web Workers |
25 days ago
Yes, there can be good performance-based reasons to call worker.terminate(). While it is true that leaving a web worker alive can save time in creating a new one later, it also consumes memory and other resources while it is idle. In some cases, having too many web workers in memory can cause the browser to crash, as you have observed in your tests. Additionally, web workers are designed to perform background tasks without blocking the main thread. If a web worker is left alive and idle, it may still consume resources and potentially interfere with other background tasks or main thread performance. Therefore, it is generally recommended to call worker.terminate() when you are done using a web worker, especially if you will not need it again in the near future. This ensures that resources are freed up for other tasks and helps prevent potential performance issues.