If you read my last post you know I recently had a bit of fun playing around with web workers. While this little experiment was fun as a simple way to learn how to use web workers it also led me to some very important conclusions about when, and when not, to use web workers.
Before I get into what I have concluded lets get on the same page. As I completed both solutions I ran them each with N ranging from 8 - 15 and timed the results. This is what I found:
|Sequential (s)||Web Workers (s)|
|N = 8||.004||.219|
|N = 9||.014||.219|
|N = 10||.056||.264|
|N = 11||.229||.479|
|N = 12||1.010||1.425|
|N = 13||5.495||6.524|
|N = 14||32.871||35.310|
|N = 15||209.461||272.359|
Now it is important to remember that there are factors other than speed that determine when a tool should be utilized. That being said, I will not be writing any posts suggesting you
"Web Workers All The Things!".
A Numbers Game?
It is clear from this that web workers will not allow us to parallelize all our code for fun and profit. However, I think there are some very good use cases.
Lately I have seen some articles stating that that offloading long running tasks to web workers will allow them to complete faster. Looking at this, the worker was slower even as the task approached 5 minutes so I would tend to disagree. Sure this may converge as tasks continued to get slower but past a second or two you already have a much more pressing issue: you're user is waiting. In the 1.5-272 seconds it took for that team of workers to complete this task, the main thread of my program could have been doing anything else!
Preventing any gap in the usability of an application is what web workers can, and should be used for. We have all seen the impact of load times on bounce rates and know that we only have a few seconds before we lose a users attention. Do you imagine a user would prefer an application that is still responsive to their actions with a process taking 1.5 seconds or an application that locks up for 1 second while some process occurs? I can tell you I don't have the data to answer this question. But, I can tell which I'd prefer and I'd be willing to run the tests to find out. Will you?