254 Chapter 10—Web Workers
computation and to output the calculated minimum and maximum altitude
along the profile. Once all sections have been calculated, the program returns
the number of points found. The website displays the number of points as well
as the time it took to calculate the profile. It would make sense to send the entire
altitude profile back to the calling program as a result, but if many sections are
used, the profile takes up a lot of memory space and slows down the program
considerably. This would not achieve the desired demo effect. Figure 10.2 shows
two profiles being calculated in parallel using web workers.
Figure 10.2 Web workers calculating two altitude profiles simultaneously
If we are creating more than one profile, we can let the web workers carry out
the calculations in parallel, whereas an analysis without web workers always has
to be done sequentially. On modern hardware, where the operating system has
multiple core processors available on the CPU, this means that the browser can
divvy up the workload between the different cores. Figure 10.3 shows this situ-
ation on a system with four CPU cores. Although the call with web workers uses
two cores to 100 percent capacity (at about 30 seconds), we can see in the sec-
ond case that without web workers only one CPU core is used to its full capacity
(at 15 seconds). The result is a marginally faster computation with web workers,
with the browser reacting to input during the computation and continually up-
dating the progress bar.