Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the event loop in Javascript executing in a separate thread?

I still believe Javascript is single threaded, but when I think about the event handling mechanism, I have some doubts.

  • Is event loop a separate thread, which pulls events one by one from queue and process.

Why I think like this is even while processing one event from queue, it can listen or it can push events to same queue. I created an example as below:

<html>
<head>
<script>
function clicked(){
    alert("clicked in between..");
}
function longRun(){
    for(var i=0;i<50000;i++){
        console.log(i);
    }
    alert("completed .... ");
}

</script>
</head>
<body>
    <input type="button" value="quick!" onclick="clicked();"/>
    <input type="button" value="long run!" onclick="longRun();"/>
</body>
</html>

When I click on long run! it will take some time to complete, but in the meanwhile if I click on quick! it will be added to the event queue and will be executed immediately after the long run event.

What is actually happening? Can anyone explain / correct me

like image 211
Tom Sebastian Avatar asked Aug 12 '15 04:08

Tom Sebastian


People also ask

Does event loop run in separate thread?

Yes, the event loop runs on the same thread as your main function. No, the event loop doesn't start processing messages until your main function returns. console.

Is JavaScript event loop single threaded?

Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.

Is event loop a loop internally in JS?

The Event Loop executes internal activities on any callbacks during this phase. Technically, there is no way to change the length or nature of this phase. During this phase, there is no mechanism in place to guarantee code execution.

Is event loop a thread?

Event Loop — Means single threaded infinite cycle which is making one task at a time and it's not only making single task queue, but it is also prioritizing tasks, because with event loop you have only one resource for execution (1 thread) so for executing some tasks right away you need prioritizing tasks.


2 Answers

Except for webWorkers (which we aren't talking about here), there is only one "user thread" per window in a browser. That means there's only one thread running your user Javascript.

This does not mean that the browser engine under the covers does not have some other threads to handle non-Javascript processing. In fact, this is very likely the case. But, since these other possible threads, never actually run any of your Javascript or affect any of your Javascript variables, they don't directly affect the JS execution environment.

Yes, these other threads may indeed insert things into the JS event queue that will be picked up later when the main JS thread is ready to process the next event.

See this answer How does JavaScript handle AJAX responses in the background? for more info and a list of related articles.

like image 116
jfriend00 Avatar answered Sep 21 '22 14:09

jfriend00


There is only one thread per tab. This thread does both JavaScript execution and screen updates. While longRun is executing, you cannot react to a click on a button; the event you registered will be fired after all 50000 iterations are complete.

To clarify: events get put on a queue. The queue is getting executed in order by the tab thread.

EDIT: And, indeed, there's workers, but they act as if they were executing in a different tab - they have their own context, and can only communicate with the invoking program using postMessage.

like image 34
Amadan Avatar answered Sep 21 '22 14:09

Amadan