Print Shortlink

Background threads in JavaScript

One of the prominent features in HTML 5 is WebWorkers. WebWorkers provide the facility to run your JavaScript code in a background thread. All the code that we write in JS are executed in the UI thread. Hence performing a long processing task in JavaScript tends to block the UI and make it unresponsive.

WebWorker introduced in HTML 5, can be used to execute your JavaScript code in the background without obstructing the UI thread. Code running in Worker thread does not have access to the DOM, data storage areas, but has access to the other core JavaScript API, can perform AJAX calls etc.,

In one of my client assignments that involved developing mobile web application, WebWorker was used to load large amount of data during the initial page load. The code that ran as a worker thread performed multiple AJAX calls and loaded the data and gave it to the UI thread which stored it in Web Sql database and consumed it whenever required. When the Worker thread was running in the background, the UI thread was busy constructing the layout components like accordion, lists etc.,

WebWorker has support from the latest versions of many browsers. It has to be used judiciously as it supposedly consumes a lot of memory.

In the next few posts let’s discuss the use of WebWorker API with simple examples.

Leave a Reply