Do HTML "script" tags get "thread" unique to each one in the browser?

Asked

Viewed 109 times

2

Separate Javascript processes in tags <SCRIPT> are worthwhile for the individual processing of each programming, or the browser joins everything in a single process?

Someone who really understands operating system and threads generated could inform me this?

Let me 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’d like the call from player video was performed independently of all operations of DOM manipulation of the tag previous. Separating into two tags <SCRIPT> that’s going to happen?

  • As far as I know Javascript is single threaded, these two pieces of code will be run sequentially and not parallel.

  • 1

    It was worth @Sergio . I didn’t know it was single and sequential. I even imagined it to be sequential, but I wasn’t sure. The html world today is very signed, you will know... hehe

1 answer

3


No. You cannot separate execution in processes or threads in this way. All engine Javascript will run in a single environment.

There may even be some situations that the browser can do some operations, usually internal things, in a thread separate. But this is his "problem". Don’t count on it.

For the programming of script you should consider that you are running all in one thread, although this is not always true especially if you are using asynchronous Apis, events and setInterval (is the hint of how to get a result next 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 the execution. At most what you can achieve is that a script coming externally by a src be 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 turn into a browser not following the pattern. Guess which browser does not give this guarantee? It is the same as always that is different from others.

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

Anyway this is a browser implementation detail and not something specified, something you might consider.

If you want simultaneous run need to use Webworkers, if the browser has this feature available.

  • Excellent answer. I liked to know the Webworkers and it was worth the tip of the setInterval.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.