7

I understand the event loop is run periodically to check if there's any events from the browser, and to check the task queue. But, how often does this happen? Is there a set interval?

I understand blocking code on a webpage will block up the event loop because JavaScript is single threaded, but in async code (with minimal blocking) how often does the event loop run?

BugHunterUK
  • 8,346
  • 16
  • 65
  • 121
  • Why the downvotes? – BugHunterUK Aug 23 '16 at 18:43
  • Wasn't me, but probably because questions at SO have to mention what problem you trying to solve, what code you used, and what error messages you got – Ruan Mendes Aug 23 '16 at 18:44
  • Why do you want to know this? As you can see in a simple test (keypress->event-> doing sth) the browser runs very fast... – Jonas Wilms Aug 23 '16 at 18:45
  • Certain events (like keypresses) arent handled in a loop. See "Interrupt" and "Interrupt Handler" – Jonas Wilms Aug 23 '16 at 18:45
  • Yes, your question is not clear. Read on internet about javascript eventlisteners. However, if you want to terminate the listener as some point of time, there are ways to unbind it. – MindGamer Aug 23 '16 at 18:46
  • 1
    The event loop runs when there's something to process in the queues. It's not like an interval in JavaScript. – JJJ Aug 23 '16 at 18:46
  • aside from the animationFrame type callback/event, nearly all events are fired immediately after the UX that triggered them. – jusopi Aug 23 '16 at 18:47
  • Who said anything about terminating? I'm interested in general at how a browser implements the event loop and how often in runs. This is more a question related to JavaScript internals. – BugHunterUK Aug 23 '16 at 18:47
  • @jusopi That wasn't the question I asked. The event loop runs either at a specific interval or at certain points if the script isn't blocking. I wanted to know the specifics. Is that not allowed on StackOverflow ... lol. – BugHunterUK Aug 23 '16 at 18:49
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop – Ruan Mendes Aug 23 '16 at 18:49
  • @JuanMendes where in that document does it answer the question? – BugHunterUK Aug 23 '16 at 18:50
  • 4
    [Here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Event_loop) – JJJ Aug 23 '16 at 18:51
  • 1
    @Juhana no that still doesn't explain how often the event loop runs. That simply explains what it does. Don't worry, I'll dig through the V8 source ... that will answer the question. – BugHunterUK Aug 23 '16 at 18:53
  • 2
    *"queue.waitForMessage waits synchronously for a message to arrive if there is none currently."* --> the event loop runs whenever there is an event to process. Again, it's not a loop that's continuously running like an interval. – JJJ Aug 23 '16 at 18:53
  • Of course there's a loop. There has to be a loop. We're talking about the event "loop". The loop that listens for events and passes them on. In it's very primitive form akin to a while loop. How often does this event loop run, at what interval? ... jeez. I can't believe people are falling over such a simple question. – BugHunterUK Aug 23 '16 at 18:55
  • No, the loop doesn't listen to the events. The loop runs whenever there's stuff in the message queue that needs processing. – JJJ Aug 23 '16 at 18:56
  • @Juhana No. When using one of the webapis, that's pushed into the task queue. The event loop pulls from the task queue. This loop that is doing that HAS to be implemented as a loop for it to function. At what interval is it running? – BugHunterUK Aug 23 '16 at 18:57
  • Webapi -> Pushes to task queue -> Event loop takes it -> Pushes onto stack. That's how it works. – BugHunterUK Aug 23 '16 at 18:59
  • 2
    The engine that has the event loop is implemented in C or C++ or some other language that doesn't have the same kind of asynchronous nature as JavaScript. The engine has a while loop that looks something like `while(queue.waitForMessage()) { ... }`. The `queue.waitForMessage()` is a *synchronous* function that blocks execution until it returns a value, and it doesn't return until it has received a message. The loop makes another iteration right after and then starts waiting for a new message. You're free to believe it's not true, but I'm afraid that's the answer you're going to get. – JJJ Aug 23 '16 at 19:02
  • 2
    The event loop in Node uses libnv. Now, you've just said it "the engine uses a while loop" ... this is what I have been saying from the start. Scroll up. Now, the question was, how often does this loop run. We're really talking internals here, and I think most people who left a comment thought I was talking about something completely different. Hence why I am confused at the downvotes because it's a genuine question. – BugHunterUK Aug 23 '16 at 19:06
  • 1
    @BugHunterUK : lets take a while loop that runs directly on the processor (not js) . How long will it need to run trough the loop once? No one can predict. It depends on the processor (how fast it is) and on interrupts that may "interrupt" ;) your loop – Jonas Wilms Aug 23 '16 at 19:10
  • Well now we're getting somewhere. As you can see that MDN page doesn't explain anything what we're discussing now which was my original question. Now, I assumed that chrome wouldn't implement the event loop to run when it likes and would have maybe specified an interval for the event loop to run. Which is why I asked the question. If you've got a complete answer to correct me please submit it. It was exactly the answer I was looking for. – BugHunterUK Aug 23 '16 at 19:12
  • 5
    A good question with ludicrous down-votes and dreadful answers. + 1 – nicholaswmin Jan 07 '18 at 21:40

1 Answers1

-5

The event thread is not triggered on intervals, but by signals from the OS that add events to the message queue, like key and mouse events, or by callbacks in your own code (like XHRs and setTimeout). See https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Event_loop

The main thread is shared between:

  • UI Layout
  • JS execution

Whenever an event occurs (XHR callback, click handler, setTimeout callback...), your JS runs, but the UI doesn't update until your JS has finished executing.

There is a way to use other threads, WebWorkers, and that code doesn't have access to the DOM, so it's thread safe and can run indefinitely.

Also note that each window object has a separate thread.

Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Doesn't answer the question buddy. What I am asking is the event loop, the one the browser implements, that pulls data from the task queue ... at what interval is this event loop running. – BugHunterUK Aug 23 '16 at 19:03
  • So your question is how often does the browser check the message queue? – Ruan Mendes Aug 23 '16 at 19:44
  • I've reposted just incase my OP wasn't clear enough http://stackoverflow.com/questions/39109205/javascript-internals-at-what-interval-does-the-event-loop-run?noredirect=1#comment65566030_39109205 – BugHunterUK Aug 23 '16 at 19:46
  • @BugHunterUK I think it's still not a good question for SO. SO questions need to show code, expected behavior, error messages. It's hard to provide an answer if you don't explain the relevance of the question – Ruan Mendes Aug 23 '16 at 19:47