permalink

1

Web Workers – Amazing Parallel JavaScript Tasking For Your Application

 

I’ve been looking at some of the cool new features being added to popular web browsers lately and I thought I’d talk about Web workers this week. Spawned from the Google Gears team’s WorkerPool Api [1], Web workers allow your web pages to run Javascript as a series background threads. Once you’ve created a worker you can communicate with them by simply posting messages to an event handler that’s been specified by the creating object. The real power behind Web workers is that you can perform certain tasks without interfering with the user interface at all – this means fewer pages "hanging" when you’re executing a ton of Ajax reqests through your UI as web worker threads are able to use XMLHttpRequest. 

 

At the moment when you want to achieve any real computation using JavaScript you need to split up your tasks into chunks and then split their execution apart using timers – this can be very cumbersome and doesn’t allow any of them to be run in parallel. A Worker is very easy to create and using the line "new Worker("script.js");" should be enough to get your script running in the background without any problems. Sound good so far? Although workers are a great addition , there a few caveats to using them. One of the biggest is that they don’t have direct access to the DOM (at least not at the moment) – the main functions you do have access to are setInterval, setTimeout and as previously mentioned – the XMLHttpRequest. The way workers should be used effectively are as follows: Create a new worker which you then assign an onmessage function to.

 

This is the message received from the client which can itself use the postMessage API to post a message back saying that something has been done or that an event has been executed. This model has been embraced for a few different reasons – It allows the worker threads to run securely as they can’t affect the parent scripts that ran them. Secondly, as the DOM is locked down from more threads, web browser developers don’t have to worry about managing the logistics surrounding large pools of threads running around crazily.

 

To summarize all of this, Web workers let us take our JavaScript processed and highly parallelize them. Don’t worry about backwards compatability – for browsers that don’t support workers, your browser will simply degrade to executing tasks in sequence but for those that do..you can expect a smoother, faster user experience. To get started using Web Workers, feel free to check out the Mozilla page on Web Workers for FireFox 3.5 below [2] or Ajaxian’s post on Workers with another code example[3]. You should also find support for workers in Safari 4 and the Chromium nightly-builds. Why not try them out today? :)

 

References
http://code.google.com/apis/gears/api_workerpool.html

https://developer.mozilla.org/En/Using_DOM_workers

http://ajaxian.com/archives/web-workers-update-for-firefox-31

1 Comment

Leave a Reply

Required fields are marked *.