Web workers
Web workers provide a way to run JavaScript code in the background on a separate thread from the main thread of a web application. Although it may seem like JavaScript is multithreaded because of its asynchronous nature, the truth is that there is only one thread. If you tie that thread up with a long running process, the web page will become unresponsive until it finishes.
In the past you could alleviate this problem by breaking long-running processes into chunks to do a little bit of the work at a time. After each chunk you would call setTimeout(),
passing it a value of zero for the timeout. When you call setTimeout()
,
what actually happens is that an event gets put into the event queue after the amount of time specified. This allows other event already in the queue a chance to get handled until your timer event makes it to the front of the queue.
If you've ever worked with threads before you will be aware that it is easy to run into concurrency issues. One thread could...