Shoving slow stuff into a Web Worker
Once you decide to end your IE support where Microsoft does (11), a whole world of opportunities opens up. One of those opportunities is Web Workers, and it is going to save my bacon.
Web Workers provide a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.
As we got more years of data for our metrics in the Quality of Life project, I’d occasionally hit a really long lag when switching between metrics. Some dev tools spelunking later and I discovered the problem.
I do a Jenks breaks calculation with the data values of a metric across all years of data. We have 12 years for some metrics now. 12 * 464 values = your Jenks calculation is going to take a long time; 1.3 seconds on my smoking fast rig, a helluva lot longer on a phone. That’s way too long to lock up a UI.
So…a Web Worker is born.
import isNumeric from '../modules/isnumeric';
Then call it in the main thread.
let worker = new Worker('./js/workers/jenksbreaks.js');
Now the rest of the app can keep trucking while Jenks breaks are being calculated. It’ll be in v3 of the Quality of Life project, which will (maybe) be ready for testing at the end of October.