Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the use-cases for Web Workers? [closed]

People also ask

What is the use of web worker?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.

What methods can be used to stop web workers?

From the main thread, you can terminate a web worker by calling the terminate() method of the Web Workers API: worker. terminate(); After terminate() is issued, the web worker is destroyed immediately without any chance of completing any ongoing or pending operations.

What are the limitations of web workers?

Limitations Of Web WorkersA worker can't directly manipulate the DOM and has limited access to methods and properties of the window object. A worker can not be run directly from the filesystem. It can only be run via a server.

What are web workers example?

Common examples of web workers would be: Dashboard pages that display real-time data such as stock prices, real-time active users, and so on. Fetching huge files from the server.


  • John Resig (of jQuery fame) has a bunch of interesting examples of using web workers here - games, graphics, crypto.

  • Another use is Web I/O - in other words, polling URLs in background. That way you don't block the UI waiting for polling results.

  • Another practical use: in Bespin, they’re using Web Workers to do the syntax highlighting, which you wouldn’t want to block your code editing whilst you’re using the app.

  • From Mozilla: One way workers are useful is to allow your code to perform processor-intensive calculations without blocking the user interface thread.

    As a practical example, think of an app which has a large table of #s (this is real world, BTW - taken from an app I programmed ~2 years ago). You can change one # in a table via input field and a bunch of other numbers in different columns get re-computed in a fairly intensive process.

    The old workflow was: Change the #. Go get coffee while JavaScript crunches through changes to other numbers and the web page is unresponsive for 3 minutes - after I optimized it to hell and back. Get Back with coffee. Change a second #. Repeat many times. Click SAVE button.

    The new workflow with the workers could be: Change the #. Get a status message that something is being recomputed but you can change other #s. Change more #s. When done changing, wait till status changes to "all calculations complete, you can now review the final #s and save".


I have used them for sending larger amounts of data from the browser to server. Obviously, you can do this with regular AJAX calls, but if this takes up one of the precious connections per hostname. Also, if the user does a page transition during this process (e.g clicks a link), your JavaScript objects from the previous page go away and you can't process callbacks. When a web worker is used, this activity happens out of band, so you have a better guarantee that it will complete.


Another Use case:

Compressing/De-compressing files in the background, if you have a lot of images and other media files that are exchanged from the server in compressed format.