Ruoyu Sun's

Thoughts on design and code

Node.js is Perfectly Fine And Probably You Don't Need it

31 Mar 2013

Yesterday, my friend told me that he planned to rewrite his app, which was content-centric with a tiny social network running fine in PHP, to Node.js. I immediately told him “Don’t do that, it’s a terrible idea”. He said “Hah, so you are one of the node haters then.” I said “No, I am not. I think Node.js is perfectly fine and I don’t hate Javascript either.”

In my friend’s case, I don’t advise him to rewrite his app in any other languages. He has been writing PHP for years and are quite proficient. He always feels like picking up something new. I think learning new stuff is great - just don’t do it by rewriting your existing app, which has complicated logic and of course, spaghetti code. Rewriting any existing code in some language you are not familiar with is almost guaranteed to fail. If you aim to make your existing codebase better, refactoring it is more practical.

I used to write an app in PHP (actually I was forced to, for various reasons). When I finally was able to get out of it, I felt that I could not bear with PHP any more and decided to rewrite it in Python, a language which I had a decent experience with. After a week of rewriting some of the component, I found out that it would take more than a month to rewrite the whole application and I could add a lot of new features in this month without rewriting, which was more important. And during that week, I found out that most of the code that I considered ugly, were actually because of the way it was written, not the language. Of course, PHP is an ugly language with bad design. But simply translating the ugly code to any other languages does not solve the problem. In fact, I sorted out the logic, organised in a cleaner way and eliminated the usage of the bad language features (which PHP has a lot), I ended up with an much cleaner codebase.

Back to Node.js, I feel that Node.js is like PHP. I am not talking about the language itself - I am talking about why it is created. PHP was created to make web programming easier and it certainly achieves that goal. That’s why it gets popular and throughout these years, it evolves a lot to overcome some of its bad design. Node.js is very similar. It was create to make writing concurrent web applications easier. It tries to achieve this by choosing a language that every web programmer has more or less experience with - JavaScript. It also chooses a coding style that is common in JavaScript - event driven and callback style (reactor model). They are probably not the best design choice, but they certainly achieves the goal. Even though callback style (or CPS, Continuation-passing style) is not a good way to organise complex logic. Luckily, the logic of most web apps are not that complicated. And in case you are not lucky you can pull out promise to help you deal with callback hell. Node.js has evolved several great libraries to help you on that.

Node.js is fine, it has its merit and it serves its niche market well. So when you start a new real time web app with not-so-complicated logic, try it. If you are developing an online game, you might want to deal with concurrency more seriously - coroutines might suit you better. If you start a content-centric or CRUD web app, you don’t need Node.js. If you write it with Rails or Django, you can develop much faster and you don’t need to deal with callbacks mess on top of your logic. The thing is, for these apps, you don’t have lots of concurrent requests. And for a single request, most of the time is spent on network transfer and perhaps database query - Node.js does not solve the essential problem here. So instead of squeezing a few milliseconds out of perhaps a hundred, you are way better off getting a SSD or a better bandwidth.

So only use Node.js when you actually need it. Don’t just use it because it is cool / it is popular/ I heard it’s fast / I want to learn something new. And by the way, the best place to try something new is on your weekend project, not on your existing project that runs perfectly fine.

Discuss on HN.

If you have comment, you can post it HN (link can be found at the end of the essay), send me an email at ruoysun AT gmail DOT com or ping me on Twitter @insraq.