In the previous post we set up a Web Worker helper function that allowed us to create a worker file, and call it using code like this:
Now, wouldn't it be nice if you didn't even have to write the worker file? To achieve this we must overcome the fact that Web Workers need to be constructed with a file name containing the Worker definition, as opposed to the function to be run. To get around this problem we just create a Web Worker that takes, as a message, a function definition and arguments, all encoded as a JSON-string.
This technique adds some code to our mission: to convert a function to and from a string requires a little bit of effort. To convert a function to a string we simply do this:
But the reverse - getting the function back from a string - is more difficult. We could try using eval:
However if you try that you will find that the performance of running the function in Chrome is abysmal: the function doesn't get precompiled and the net result is that execution time is more than 10x slower. Another alternative is constructing the function using the new Function syntax. In the following table I compare the performance of each (all in milliseconds - lower is better):
Native | Eval | new Function | |
---|---|---|---|
Chrome 9 | 207 | 2955 | 204 |
IE 8 | 4078 | 4890 | 4047 |
Opera 11 | 240 | 1080 | 240 |
Firefox 3.6 | 341 | 342 | 336 |
In all cases, constructing the function using new Function gives the same performance as a natural JavaScript function, so we'll use that instead of eval. In the following code we see how to convert a string-encoded function to a real function using the Function constructor. It just involves manipulating the function's string to get the function body, and name of the function's argument, and then pass those to the Function constructor.
And combining this knowledge into a "generic" Web Worker, we have the following code:
Note that in the above worker we attach to the message event using the standard addEventListener syntax. That is much nicer than the old school method of adding a function to the onmessage property, and allows us to attach multiple listeners if needed.
To consume this Web Worker we must serialise the a function to be run, and its arguments, so they can be passed in a message. Our $.work function can do that for us. We'll also add one other detail: make it cross-browser compatible by synchronously executing the action when there is no Worker definition.
To define the code, you can write any function that takes a single parameter:
And running it then becomes this succinct beauty:
Performance
To try out the performance I'm going to do the following 3 tests in the usual 4 browsers:
- Run the findPrimes function in the UI thread (no workers involved)
- Run the findPrimes function in a Web Worker (keeping the UI thread free)
- Run the findPrimes function in two Web Workers (splitting the calculation into two equal parts)
UI thread | One worker | Two workers | Observations | |
---|---|---|---|---|
Chrome 9 | 4915 | 4992 | 3268 | CPU at 50% with 1 worker, 100% with 2 |
Firefox 3.6 | 7868 | 7862 | 5289 | CPU at 50% with 1 worker, 100% with 2 |
Opera 11 | 5754 | 5780 | 5676 | CPU at 50% in both cases (but UI thread is free) |
IE 8 | 108689 | same | same | CPU at 50% in all cases (UI thread is always used) |
In the above tests we can see that execution always takes the same time in a Web Worker as in the UI thread. In Chrome and Firefox, we see that executing Web Workers concurrently gives a nice performance improvement by taking advantage of multiple CPUs on the user's machine. These are very positive results, especially considering the overhead in constructing and messaging the Web Workers.
Chrome
This technique of wrapping Web Workers works really well in Google Chrome, even though, as we saw in Part 1, Chrome has the largest overhead in constructing a Web Worker object. As you would expect, Chrome makes use of multiple cores by running the Web Workers in separate threads and we can achieve a good speed-up in performance on multi-core machines vs single-core machines.
Firefox
Firefox also has great performance. There is a good speed-up on multi-core machines, and additionally, Firefox has a low overhead in constructing Web Worker objects.
Opera
Although Opera does support Web Workers, it doesn't seem to run them in their own threads - in the table above we can see that the performance when running multiple workers is no better than running a single worker, when on a multi-core machine. I noted that the CPU usage maxed out at 50% on my dual-core machine even though I was running multiple workers. I'm sure Opera will resolve this in the future though, and using Web Workers still frees up the UI thread and makes the browser responsive during long-running calculations.
Internet Explorer
In IE, since we are executing exclusively in the UI thread, long-running calculations will result in the message "A script on this page is causing Internet Explorer to run slowly". This will occur if your worker function executes more than 5 million statements. By way of workaround I can only suggest the following:
- Split the worker into multiple smaller workers to ensure the 5 million-statement limit is not reached. You should endeavour to provide user feedback regularly to let the user know that the application has not crashed.
- If you have control over the client's registry (eg. a company internal application) then the limit can be changed, although that is a bad idea because the browser will be unresponsive for a long time.
- Offer an alternative version of your application to IE users, which is not as computationally intensive. Inform the users that they can use the full version in another browser.