Web Workers: Multithreaded Programs in JavaScript

Web Workers: Multithreaded Programs in JavaScript

Ido Green

Language: English

Pages: 62

ISBN: 1449322131

Format: PDF / Kindle (mobi) / ePub

Web apps would run much better if heavy calculations could be performed in the background, rather than compete with the user interface. With this book, you’ll learn how to use Web Workers to run computationally intensive JavaScript code in a thread parallel to the UI. Yes, multi-threaded programing is complicated, but Web Workers provide a simple API that helps you be productive without the complex algorithms.

If you have an intermediate to advanced understanding of JavaScript—especially event handling and callbacks—you’re ready to tackle Web Workers with the tools in this example-driven guide.

  • Start creating Web Workers and understand what they can and can’t do
  • Determine which browser versions support the API
  • Use dedicated Web Workers for tasks that consume a lot of CPU, such as data parsing
  • Explore use cases for creating inline Workers, such as encapsulating a web app in one page
  • Create a shared Worker to communicate multiple web app instances to the server, and other uses
  • Learn best practices for debugging Web Workers
  • Apply Web Workers within the server-side Node environment

CherryPy Essentials: Rapid Python Web Application Development

CSS Text

Beginning Django E-Commerce

HTML, XHTML, and CSS: Comprehensive (6th Edition)

Java EE 7 Essentials
















government agencies, and individuals. Subscribers have access to thousands of books, training videos, and prepublication manuscripts in one fully searchable database from publishers like O’Reilly Media, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, Course Technology,

“unresponsive script” dialogs like the ones shown in Figure 1-1 and Figure 1-2. Figure 1-1. The warning dialog for “unresponsive script” in Windows Figure 1-2. The warning dialog for “unresponsive script” in MacOS What Can Web Workers Do? If your web application needs to complete a task that takes more than 150 milliseconds, you should consider using a Web Worker. If your app needs to feel like a native one, you should even consider setting the bar at around 80 milliseconds.

worker.onmessage = function(event) { console.log("Called back by the routes-worker with the best route to the pub"); } You can also keep in touch with your Workers using addEventListener: var worker = new Worker('routes.js'); worker.addEventListener('message', function(event) { console.log("Called back by the routes-worker... with the best route to the pub") }, false); worker.postMessage(); // start the worker. What Web Workers Can and Can’t Do Workers don’t have access to the DOM of

encoding/decoding the object value on each side (web app page and the worker). This means that you may pass JSON or any other serialized data. For example: postMessage ({'cmd': 'start', 'time': Date.now() }); Loading External Scripts To load external script files or libraries into a Web Worker, use the importScripts() global function. This function takes strings as arguments for the resources to import. If you give it zero URLs nothing will be invoked. However, if you give it one or


  • Worker: Found another prime: " + n + "
  • "); } postMessage("

    Worker: Done

    "); }