Threading sounds complicated and some development languages make it tricky, but you’ll be pleased to hear that JavaScript’s implementation is good and the W3C working draft is stable. Web workers offer huge client-side performance gains, but there are a few things to note before we start…
Web Worker Restrictions
Web workers operate independently of the browser UI thread so they’re not able to access many of the features JavaScript developers know and love. The primary restriction is that web workers have no access to the DOM; they cannot read or modify the HTML document. In addition, you cannot access global variables or JavaScript functions within the page. Finally, access to some objects is restricted, e.g. window.location properties are read-only.However, web workers can use standard JavaScript data types, handle XMLHttpRequest (Ajax) calls, use timers and even import other workers. They are ideal for time-consuming tasks such as analyzing large blocks of data, game AI logic, ray-tracing, etc.
Web Worker Browser Support
At the time of writing, all the recent editions of Firefox, Chrome, Safari and Opera support web workers to some extent. Guess which browser is missing?Unsurprisingly, web workers are not implemented in any version of Internet Explorer. Even IE9 does not offer support but I suspect/hope it will be implemented in the final release. Until that time, you have three options:
- Forget about web workers for another year or two.
- Accept that your application will break in IE.
- Implement your own web worker shim which falls back to timer-based pseudo-threading or array processing. That may not be possible or advisable in all applications.
What is a Web Worker?
A web worker is a single JavaScript file loaded and executed in the background. There are two types:- Dedicated workers: these are linked to their creator (the script which loaded the worker).
- Shared workers: the allow any script from the same domain (somesite.com) to communicate with the worker.
Creating a Dedicated Web Worker
To create a dedicated web worker, you pass a JavaScript file name to a new instance of the Worker object:- var worker = new Worker("thread1.js");
Communicating With a Dedicated Web Worker
Since the web worker cannot access the DOM or execute functions within the page’s script, all communication is handled through an event interface. The web page script passes a single data argument via thepostMessage()
method and receives one back via an onmessage
event handler, e.g.pagescript.js:
- var worker = new Worker("thread1.js");
- // receive messages from web worker
- worker.onmessage = function(e) {
- alert(e.data);
- };
- // send message to web worker
- worker.postMessage("Jennifer");
onmessage
event handler and postMessage()
method accordingly:thread1.js:
- self.onmessage = function(e) {
- self.postMessage("Hello " + e.data);
- };
Handling Dedicated Web Worker Errors
Web worker code is unlikely to be perfect, and logic errors could be caused by the data passed by the page script. Fortunately, errors can be caught using an onerror event handler. The handler event is passed an object with 3 properties:- filename: the name of the script which caused the error;
- lineno: the line number where the error occurred; and
- message: a description of the error.
- worker.onerror = function(e) {
- alert("Error in file: "+e.filename+"\nline: "+e.lineno+"\nDescription: "+e.message);
- };
Loading Further JavaScript Files
One or more additional JavaScript libraries can be loaded within a web worker usingimportScripts()
, e.g.- importScripts("lib1.js", "lib2.js", "lib3.js");
Stopping a Dedicated Web Worker
The web worker thread can be stopped using theclose()
method, e.g.thread1.js:
- self.onmessage = function(e) {
- if (e.data == "STOP!") self.close();
- };
No comments:
Post a Comment