Summary 259
if (evt.data.useWorker) {
postMessage({task:'update', status:i, id:evt.data.id});
}
if (evt.data.useWorker) {
postMessage(d);
}
else {
return {data:d};
}
The client-side analysis of image data conserves server capacity and network
bandwidth. Provided there is suitable hardware equipment on the client side,
this could give users the option of digitalizing altitude profiles on an image with
an alpha channel and then graphically representing these in realtime.
If this has whet your appetite for web workers, please do not forget that using
workers requires more resources than scripts working without workers. Data
transfer with messages between a worker and the calling script is especially slow-
er than in a script with direct access to the resources.
Summary
This chapter introduced the concept of scripts running parallel in the browser. In
desktop applications these are known as threads; in the browser they are called
web workers. Access to the elements of the website is subject to certain restric-
tions, but information can comfortably be exchanged between the calling script
and the individual workers through the concept of message passing.
Web workers are particularly useful for large web applications where processes
are running in the background and should not block user input. Think for exam-
ple of automatic saving while you are working on a document or coloring source
code while you create it, as demonstrated by Mozilla’s Web Editor Ace.