One of the new cool features of Firefox 3.5 is the support of Web Workers (see the WhatWG specification).

Web Workers give you a way to execute a part of your Javascript in a thread. That means smoother pages.

I've written a little demo showing Web Workers in action.

But first, a little explanation of the code: I've developed the Simulated Annealing algorithm, which is useful for finding the shortest path between several points (displayed on a <canvas/>).

Here is a demo (without Web Workers). Click on the load button, then on the start button.

The main demo

So, the main demo (with Web Workers) uses this algorithm to find the shortest path of 6 sets of points. The algorithm could be executed in a Worker, or not.

See the difference! A bit faster and way smoother (it doesn't freeze your web page, the page is still alive during the process). Notice the Animated PNG: it gets stuck during the process if you don't use Workers.

Just one rule for Workers: you can't play with the DOM inside a Worker, but you can delegate DOM modifications to the main thread (see postmessage/onmessage methods).

Why you should use Web Workers

Since you can now do some amazing things with HTML5 (canvas, video, ...), you may need to write some heavy algorithm. It could be easier to execute every non-DOM related code in a thread. It will make your code faster, but above all, it will make your web application smoother, and the code cleaner (to avoid Spaghetti code)! Note that you can also use a XMLHttpRequest in a Worker.

Be creative ;)