Web Workers

Published August 2, 2014

Web Workers. The web's answer to threading. I'm disappointed to learn, after much investigation, that for my own practical purposes, web workers are useless. This appears to be a contrarian opinion - most articles discussing web workers praise their existence while completely skipping over the practicality of actually benefiting from them.

They have two main problems:

1. A worker cannot access the DOM. This is actually less of a problem than it initially appears, because having your GUI exist only in one thread is a reasonable limitation. But sometimes building DOM really is a bottleneck and it would be nice if you could at least build isolated fragments in workers.

2. Workers cannot share data; they are entirely separate beings. In extremely contrived circumstances, you can transfer ownership of a piece of data from one thread to another. This is not a generally useful piece of functionality, because you need to be using objects that implement the Transferable interface. You're probably wondering "what's the transferable interface?" - exactly - it's not exactly widely used.

This leaves you in the position that you need to pass copies of your data through to web workers. Copies are passed via a JSON-like process, by which I mean that objects get stripped of their classes, so you also probably need to use your own serialization augmentations to re-assemble data into real objects with correct prototypes. At this point, not only have you taken on a lot of extra work, you've also most likely killed the performance of your application by bogging it down in communication overhead, which seems like an unfortunate result when the entire point of this process was to speed it up.

In short, web workers have a very narrow use case: they are useful if and only if your problem consists of a lot of computation and very little data, otherwise the communication overhead will be greater than the gains made by using more threads. I'm not denying this use case exists, but I am denying that the majority of JS apps which could benefit from threads fall into this category.


If anyone disagrees with this I'd be interested to hear why. Most of the articles about web workers seem to vaguely allude to the (in my opinion extremely poor) solution of using transferable objects, but completely omit the practicality of using them, which almost makes me feel like I'm missing something...

Filed under: javascript, programming

Talk is cheap

Leave a comment:

HTML is not valid. Use:
[url=http://www.google.com]Google[/url] [b]bold[/b] [i]italics[/i] [u]underline[/u] [code]code[/code]
'