pouët.net

javascript event loop is for ponies.

category: code [glöplog]
 
Hello everybody. I'm having a bit of trouble understanding conceptually how the javascript runtime environment works.

I think I understand that there is a queue of work to be had and function calls get put in that queue in the order they're executed, I also think I understand that some functions require i/o and that they're taken out of the queue on a separate thread until the i/o completes at which point they're put at the beginning of the queue to be executed first.

Is that how javascript works internally or do I have it completely wrong?
added on the 2013-11-21 19:46:22 by sigflup sigflup
are you talking node.js or browser?
added on the 2013-11-21 20:19:40 by psenough psenough
hmm... node.js. How is it different in the browser?
added on the 2013-11-21 20:20:50 by sigflup sigflup
well, i'm not an expert in that under the hood stuff, but node.js is a special subset of v8 extended for event driven i/o, and that it is non blocking. so i assume the regular v8 does do some blocking.

but i'm also pretty sure p01 will arrive soon and shine much better light to this discussion. :)
added on the 2013-11-21 20:24:02 by psenough psenough
Browsers have more things to deal with that change this answer. See this StackOverflow answer as to why. Also, each browser could have different behavior for things all browsers implement that's not explicitly specified in a spec somewhere.* Within Node.js you have one execution environment that's always going to be the same, everywhere.

* on the assumption that if one exists they tried to comply with it; for simplification of argument.
Quote:
by AMcBain:
this answer

how this question is answered*
Yup node.js is quite unique since it is built on a single Javascript VM. The core idea of node is to do everything you can event driven, and I/O should be async too. Of course you can do some stuff in a synchronous way but the uses cases are limited.

I don't know the details of what goes into threads and what doesn't, but allegedly node.js doesn't use OS threads so much. One can spawn a new process to go multi-core-CPUs-happy. Really the main idea is the non-blocking I/O + event driven which prevents dead-locks and thus allow to scale rather easily.

Browsers have a few more things on their plate: user interface, layout, painting, style recalculation, plugins, ... unfortunately many things are synchronous or blocking. And of course there are many different JS and rendering engines + different networks stacks, rendering back ends, ... and the "glue" around that actually makes a browser a browser.
added on the 2013-11-21 21:48:57 by p01 p01

login