HTML5 Web Worker v/s setInterval()

Hi all,

In HTML5 we have new Web Worker feature but at first look I think its similar like as setInterval() then why they introduce Web Worker. But no there is one major difference between both is: In JavaScript all execution process remains inside a unique thread while Web Worker supports multi-threading.

Before the workers…

In JavaScript, a long running processing will freeze the main window and blocking the “UI Thread”. This is the main thread in charge of handling all the visual elements and associated tasks: drawing, refreshing, animating, user inputs events, etc. Sometimes you may notice that due to long JavaScript processing browser throws unresponsive script warning as:
5939882671_3bf264fd60

To overcome single thread limitation HTML5 is bringing us true multi-threading capability via Web Workers. We can execute the long processing script without freezing the main window. The Web Worker execution will not affect the main “UI Thread”.

When’s the good time to use Web Worker

Any script that you want to execute in the background is a good candidate to run as a Web Worker. Think about the sorting of large arrays, parallel processing, and I think you’ll see where Web Workers could be a big help.

Web Worker Limitations

Web Workers operate independently of the main browser UI thread so they’re not able to access many of its objects. One limitation is that Web Workers cannot access the DOM, so they can’t read or modify the HTML document. In addition, they can’t access any global variables or JavaScript functions within the main page.

Please click to know more about Web Worker: The Basics of Web Worker

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s