HTML "script" tags receive exclusive "thread" for each one in the browser?

2

If separating JavaScript processes into tags <SCRIPT> are worth for individual processing of each schedule, or does the browser put everything together in a single process?

Does anyone who really understands operating system and generated threads could tell me this?

I'll be clearer. Imagine the following code:

<script language="javascript">
   $(document).ready(function(){
          operações gerais aqui de manipulação de DOM
   });
</script>
<script language="javascript">
   $(document).ready(function(){
      uma chamada de player de um video
   });
</script>

I would like the video player call to be performed independently of all previous DOM manipulation of the tag . Separating into two tags <SCRIPT> will this happen?

    
asked by anonymous 30.12.2014 / 12:13

1 answer

3

No. you can not separate execution in processes or threads in this way. All engine JavaScript will run in a single environment.

There may even be some situations where the browser can do some operations, usually internal things, in a separate thread . But this is a "problem" of it. Do not count on this.

For programming script you should consider that you are running everything in a single thread , although this is not always true especially if you are using asynchronous APIs, events and setInterval (this is how to get a result close to what you want).

Each block <script> is executed sequentially and not in parallel. Even the order that each block appears on the page influences execution. At most what you can achieve is that a script coming externally by a src is loaded in parallel, but its execution will be sequential respecting the order in which it appears on the page.

Then the execution order is guaranteed ... until you run into a browser that does not follow the pattern. Guess which browser does not give this guarantee? It is the same as always that is different from the others.

It seems that HTML allows this to be specified with <script type="text/javascript" async src="script.js"></script> but I do not know how the implementation is in browsers. Note that this refers only to the file load (possibly the parse and compile / JIT as well).

Anyway this is a detail of the implementation of the browser and not something specified, something you can consider.

If you want to run concurrently you need to use WebWorkers , if your browser has these features available.

    
30.12.2014 / 12:45