I just watched the following video: Introduction to Node.js and still don\'t understand how you get the speed benefits.
Mainly, at one point Ryan Dahl (Node.js\' cre
I know nothing about the internal workings of node.js, but I can see how using an event loop can outperform threaded I/O handling. Imagine a disc request, give me staticFile.x, make it 100 requests for that file. Each request normally takes up a thread retreiving that file, thats 100 threads.
Now imagine the first request creating one thread that becomes a publisher object, all 99 other requests first look if there's a publisher object for staticFile.x, if so, listen to it while it's doing it's work, otherwise start a new thread and thus a new publisher object.
Once the single thread is done, it passes staticFile.x to all 100 listeners and destroys itself, so the next request creates a fresh new thread and publisher object.
So it's 100 threads vs 1 thread in the above example, but also 1 disc lookup instead of 100 disc lookups, the gain can be quite phenominal. Ryan is a smart guy!
Another way to look at is is one of his examples at the start of the movie. Instead of:
pseudo code:
result = query('select * from ...');
Again, 100 seperate queries to a database versus...:
pseudo code:
query('select * from ...', function(result){
// do stuff with result
});
If a query was already going, other equal queries would simply jump on the bandwagon, so you can have 100 queries in a single database roundtrip.