4

This is a question about JavaScript internals.

Lets say I have 10 async tasks that all take x number of seconds to run. Whilst waiting for a response the script is idle.

In the background the JavaScript engine is asking "Is there anything on the task queue". To my understanding this is a loop. Hence, event loop. I know in Node this is implemented with Libuv. I've read this article which explains a little: https://nikhilm.github.io/uvbook/basics.html

Do JavaScript engines place any restriction on how often this event loop runs in order to balance out performance of the application? Does it run at a set interval?

If I'm off with anything, please correct me. I was purely interested at what interval this event loop runs.

9
  • 2
    It runs at fast as it can Commented Aug 23, 2016 at 19:28
  • if you setTimeout with zero from a function with itself, it fires about 250 times a second in V8. other sources of interruption (eg. ajax) are typically not "pinged" at all, but use lower-level flow controls with sub-ms granularity. Commented Aug 23, 2016 at 19:34
  • "restriction on how often this event loop runs in order to balance out performance of the application" - do you you mean to ask whether they are artificially slowing it down? No, why would they? Commented Aug 23, 2016 at 19:35
  • 1
    @Bergi: setTimeout (et al) is about the only "js internal" where a granularity applies... Commented Aug 23, 2016 at 19:41
  • 1
    @Bergi according to MDN, if the delay value is 0, the function is executed on the next event cycle. Commented Dec 1, 2020 at 4:52

1 Answer 1

2

There is no loop per se in the JavaScript side. There is one in libuv though. Basically libuv will wait until the closest timer hits or an i/o operation happens. Then it will fire a callback in C, which calls the C++ function Node passed and then this triggers JavaScript code to be executed.

Have a look at this presentation, specially the section starting at slide 33.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.