0

Possible Duplicate:
JavaScript multithreading

When I open a web page in browser, how many threads are started to render the DOM and execute the Javascript code?

Is it possible to execute Javascript functions with multi-threads?

Community
  • 1
  • 1
Jeffrey
  • 4,436
  • 9
  • 38
  • 54
  • possible duplicate of [JavaScript multithreading](http://stackoverflow.com/questions/7639224/javascript-multithreading), http://stackoverflow.com/questions/2145829, http://stackoverflow.com/questions/39879, http://stackoverflow.com/questions/2734025 ... – Tomasz Nurkiewicz Jul 26 '12 at 12:16

6 Answers6

3

You can make the use of webworkers in modern browsers

http://www.html5rocks.com/en/tutorials/workers/basics/

Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
0

The short answer is no, you cannot run multithreaded code in javascript.

The longer answer is that you can simulate it. This question on SO might be helpful: Multithreaded JavaScript how to?

Community
  • 1
  • 1
entropy
  • 3,134
  • 20
  • 20
0

Javascript code runs in a single thread . You can not start seperate thread but you still can use asynchronous functions to achieve that like setTimeOut and setInterval .

radhe001
  • 324
  • 1
  • 3
  • 16
0

In general there is only one UI thread, but you can run JS in parallel with WebWorkers in HTML5.

Madman
  • 3,171
  • 2
  • 32
  • 44
0

You can use WebWorkers for threads, but it cant work with dom. Also you may try simulate it by setInterval

Enleur
  • 7
  • 1
  • 4
0

Another idea that people use at times is to make use of iframes. If your application can be modeled as set of iframes(like facebook), you can leverage this. Google chrome is highly optimized for this. Javascript in each frame executes in a separate execution context and hence can be parallelized.

  • What about other browsers, like IE, firefox, safari? Do thye provide the optimization for iframes? – Jeffrey Jul 27 '12 at 04:50
  • Yup.. Every browser runs javascript in each iframe in a separate execution context and hence in parallel. This is a very popular technique. The downside is that the browser has to make multiple HTTP requests, x for each frame. Go through this more some more optimizatios that the iframes provide: http://www.aaronpeters.nl/blog/iframe-loading-techniques-performance – Satyam Shekhar Jul 28 '12 at 06:01