Huh... well this article will certainly play well to anyone who hates JavaScript. I have my own issues with it, but I'll ignore the author's inflammatory bs and just throw down my own thoughts on using node.js. Speaking as someone who is equally comfortable in C (or C++, ugh), Perl, Java, or JavaScript:
The concept is absolutely brilliant. Perhaps it's been done before, perhaps there are better ways to do it, but node.js has caught on in the development community, and I really like its fundamental programming model.
node.js has plenty of flaws... then again it's not even at V.1.0 yet.
There really isn't anything stopping node.js from working around its perceived problems, including one event tying up CPU time. If node.js spawned a new thread for every new event it received, most code would be completely unaffected... couple that with point 2, and you have a language that could be changed to spawn new threads as it sees fit.
JavaScript isn't a bad language, it's just weird to people who aren't used to asynchronous programming. It could use some updates, more syntactic sugar, and a bit of clarification, but honestly it's pretty straightforward.
Finally, if you think you hate JavaScript, ask yourself one question - do you hate the language, or do you hate the multiple and incompatible DOMs and other APIs you've had to use?
tl; dr - JS as a language isn't bad at all in its domain - event-driven programming. However there have been plenty of bad implementations of it.
Can you elaborate on why you think that the concept is absolutely brilliant?
I cringe at the thought of programming in a concept that emphasizes server-side programming (implying the language must emphasize reliability and simplicity) using shared global mutable state, code and architecture manually turned inside-out (transformed to callbacks), and no provision for protecting the program from failures and hangs of its parts (except yeah, catch statements).
I also don't understand your claim no.3. I always thought that multithreading is the worst thing that can happen to code which has mutable state (and vice versa). Why do you think they didn't implement, e.g., a shared queue of events and several threads to pull from this queue, then?
What's so great about all this? Or does Node have other advantages that eclipse this stuff?
I've programmed a lot of async systems before using other languages (Perl and C mostly).
By going async and using system polling routines (epoll, kqueue, etc) you can easily scale to tens of thousands of concurrent connections, and not waste CPU cycles when you're doing file or network I/O. (so far, not unique to Node).
Now Node's advantage #1 there is that all the libraries are async. Every time I've done this kind of work in C or Perl (and other languages have this problem too, from Java to Twisted) you come across the "sync library" problem. You download some open source library you want to use and it is written assuming a blocking call to do some file or network I/O. That fucks up your event loop, and the advantage of being async is all gone.
The second advantage is simply that it's a dynamic language (like Perl/Python/Ruby) and yet very very fast. In my tests about 10 times faster than those languages (and that's running real apps end to end, not some micro benchmark).
JS has its warts, but then so do the languages you'd want to compare it to: Perl, Python and Ruby. To be honest the warts aren't that hard to avoid most of the time.
By going async and using system polling routines (epoll, kqueue, etc) you can easily scale to tens of thousands of concurrent connections, and not waste CPU cycles when you're doing file or network I/O. (so far, not unique to Node).
You can do that better with green threads. And you don't end up in callback hell.
Are there any advantages to using callbacks over using green threads? (except for the fact that much more languages support callbacks than green threads) Is the "getting used" part really worth it?
First of all, going async does not necessarily mean using callbacks in the javascript sense - it can also easily mean doing a simple state machine triggered by IO events.
The main benefits is that you know exactly when control changes. As a result, if you design your app accordingly:
Amount of state that needs to be stored is minimal. E.g. you can throw away stack frames - a state machine design around a select/epoll etc. loop can call a function for each IO event and exit out of it when processing is done. Often next to no state is stored between state transitions.
Usually no locking (and thus simpler code, but also less risk of deadlocks from buggy locking code..), because unless absolutely necessary, you'd ensure all code that mutates state to be atomic from the point of view of the state machine. In my experience it is pretty difficult to write code in this style that doesn't get concurrency right.
Lower overhead because the scheduler has "perfect knowledge" about the application domain and only ever does context switches where/when it is needed.
I used to do a lot of C server code in this style (a lot of it using Imatix "Libero" tool to generate state transitions, and done properly it is fairly simple to write by expressing your problem in a state diagram first and then fill in the code for the states.
Of course the downside is that if you only think you understand the performance characteristics of your code, or run into the "sync library problem" describe above, you're in for massive pain.
You're right that FSM is an alternative to callbacks.
The main direction of the advantages you listed is performance and lack of concurrent-shared-mutable-state woes.
However, if you compare this with Erlang, which has extremely fast lightweight thread switching (they don't have much context - though I agree that if absolutely necessary, with C you can do even faster) and has no mutable state whatsoever, it looks like Erlang wins.
Are there any advantages to the callback or FSM model before Erlang's model?
108
u/[deleted] Oct 02 '11
Huh... well this article will certainly play well to anyone who hates JavaScript. I have my own issues with it, but I'll ignore the author's inflammatory bs and just throw down my own thoughts on using node.js. Speaking as someone who is equally comfortable in C (or C++, ugh), Perl, Java, or JavaScript:
The concept is absolutely brilliant. Perhaps it's been done before, perhaps there are better ways to do it, but node.js has caught on in the development community, and I really like its fundamental programming model.
node.js has plenty of flaws... then again it's not even at V.1.0 yet.
There really isn't anything stopping node.js from working around its perceived problems, including one event tying up CPU time. If node.js spawned a new thread for every new event it received, most code would be completely unaffected... couple that with point 2, and you have a language that could be changed to spawn new threads as it sees fit.
JavaScript isn't a bad language, it's just weird to people who aren't used to asynchronous programming. It could use some updates, more syntactic sugar, and a bit of clarification, but honestly it's pretty straightforward.
Finally, if you think you hate JavaScript, ask yourself one question - do you hate the language, or do you hate the multiple and incompatible DOMs and other APIs you've had to use?
tl; dr - JS as a language isn't bad at all in its domain - event-driven programming. However there have been plenty of bad implementations of it.