10.2 Search for Leap Years 251
For web workers, the specification distinguishes between Dedicated Workers and
Shared Workers; the second category, Shared Workers, is able to receive messag-
es from different scripts and send their own messages to various scripts. In this
chapter, we will only address the first variety, Dedicated Workers; for information
on Shared Workers, please refer to the relevant sections in the specification at
http://dev.w3.org/html5/workers/#shared-workers-introduction.
Because this specification on web workers is still in an early stage and the exist-
ing implementations in WebKit and Firefox are still incomplete, we will omit a
detailed description of the API and instead present you with two introductory
examples of the way web workers function.
10.2 Search for Leap Years
Both prime numbers and the Fibonacci sequence have already been calculat-
ed sufficiently with web workers (you can easily find the relevant examples via
Google). We want to turn to another, similarly exciting, task. In the first example,
we will search for leap years since 1.1.1970. Because this task would only take a
few fractions of a second on modern hardware and would not demonstrate the
capabilities of web workers, we will make it difficult for our program. It is sup-
posed to check for very short time spans (seconds or minutes) if it is February
29 and therefore a leap year. A selection for the step size of the time span is re-
quired, because different hardware will execute the program at different speeds.
Figure 10.1 shows the output on a weak CPU after several seconds.
Figure 10.1 Web worker searching for leap years