My JavaScript book is out!
Don't miss the opportunity to upgrade your beginner or average dev skills.

Monday, February 13, 2012

Web Workers - Current Status

A quick one about workers after few tests.

Mobile

Workers are apparently nowhere in Android devices stock browsers. The only one able to bring workers seems to be Chrome in ICS.As alternative, both Opera Mobile and Firefox Beta work without many problems.About iOS, version 4 does not support workers while version 5 does and quite well.

Desktop and Data URI

Workers are almost fine everywhere except IE9 ( surpriiiiiiiiiise ) but there is one thing not a single browser does except again Firefox and Opera, accepting "data:application/javascript;" with or without base64 encoded code.On mobile side this is supported again by Opera Mobile and Firefox Beta without problems but on desktop, and not only ...

Safari works only with external files while inline data uri are supported only via file protocol

WebKit nightly does not support inline data uri even through File protocol

Why Bother With Data URI

Quite simple, Workers are a mechanism to detach some logic from the main thread and execute it in the background. The possibility to create inline Workers means we could create a sort of Threads manager, delegating runtime ad-hoc functions to perform certain tasks handling all requests from and to the main page.

Workers Until Now Are Not Good

Not only the serialization and deserialization problem does not scale with large amount of data, probably the only reason you would think to use a worker for some job, but the DOM security exception thrown with data URI and for no reason on earth is yet another limit for this technique.Current status, except for data URI, is that you may need webkitPostMessage rather than simply postMessage in order to at least optimize data transfer between global objects, but on the other hand, the dream we all have about "HTML5" keeps fading out every time I understand I need to prefix something, either if I use CSS3 or JavaScript (i.e. requestAnimationFrame).

//e.g. in chrome, may need other prefixes elsewhere//open up your developer tools and you can enter this on the console at any page//or you could put it into a page itselfvar bb = new WebKitBlobBuilder();bb.append('self.onmessage=function(event) { self.postMessage("I live!") };');