Question

If an HTML page has several script elements, each with an src attribute, is it possible for a browser to run each of these scripts on a separate thread or will they be executed in some sequence?

I know that any event handlers that have been registered will run on a single-threaded event loop, but are browsers "permitted" to run the scripts themselves concurrently?

My sense is no, but where would such a rule even be defined? Would it be part of the HTML specification? Or is such behavior omitted, leaving browser writers free to concurrently load scripts if they wish?

Was it helpful?

Solution

Normally execution is locked to one thread and scripts will be executed in the order they appear in the document. However, you can run scripts in background threads using Web Workers.

More info can be found in the W3C spec: http://www.w3.org/TR/workers/

Or on MDN: https://developer.mozilla.org/en-US/docs/Web/Guide/Performance/Using_web_workers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top