3 ms·
IMHO, Node.js will not directly compete with Rails/PHP as long as it forces programmers to program in an async style. As an example, async style makes it imposs
by olalonde 15y ago
IMHO, Node.js will not directly compete with Rails/PHP as long as it forces programmers to program in an async style. As an example, async style makes it impossible to implement lazy loading (a technique commonly used by ORMs). Furthermore, Node.js is basically based on the idea of cooperative multitasking[0] which carries a certain trust issue in large teams or projects with lots of third party dependencies.
There is definitely a niche for Node.js but it's not likely to become a direct competitor to Rails.
FWIW, I'm currently writing a web app with Node.js and have written multiple Node.js modules and native extensions.[1]
[0] http://en.wikipedia.org/wiki/Computer_multitasking#Cooperative_multitasking.2Ftime-sharing http://en.wikipedia.org/wiki/Computer_multitasking#Cooperati...
[1] http://syskall.com/how-to-write-your-own-native-nodejs-extension http://syskall.com/how-to-write-your-own-native-nodejs-exten...
- edwinnathaniel 15y agoSeems like node.js was invented for back-end, system programming, and network communication type of software systems, not optimized for typical web-app.
- wahnfrieden 15y agoWhy does it make lazy loading impossible?
- olalonde 15y agoA typical ORM allows you to do stuff like this: var user = orm.load('User', 'some unique id...'); console.log(user.name + ' was referred by ' + user.referrer.name); where user.referrer is another User instance. Since the user.referrer object might be a bit slow to load from the DB, we want to delay its loading until we actually use it (lazy load it) and not load it at all if we don't use it. This is not possible in Node.js because every I/O calls (i.e. database calls) are async. In other words, we have no guarantee user.referrer will be loaded in time (before console.log() outputs the result). Every time we want to load something from the DB, we need to supply an explicit callback which will be called one the DB query is completed. Of course, there are several ways to achieve the same result in async style, but none of them are transparent to the programmer. You will either have to load the referrer object upfront or supply a callback whenever you need to load it. For example: orm.load('User', 'some unique id...', function(user) { user.referrer.onLoad(function() { console.log(user.name + ' was referred by ' + user.referrer.name); }); });
- wahnfrieden 15y agoAh yea - so it's not impossible, it just makes typical usage overly cumbersome. Maybe this is something a coffeescript extension or analog could help solve.
- olalonde 15y agoWell, it is impossible to transparently lazy load from a database. CoffeeScript won't solve the problem since it is still ultimately limited by Node.js' async libraries. A "solution" would be to cheat and write a sync database API but then, you would break the assumption that everything is async with Node.js, making it harder to work with other programmers or use third party modules.
- xtrumanx 15y ago> CoffeeScript won't solve the problem since it is still ultimately limited by Node.js' async libraries. I wouldn't be too sure about that. .net 4.5 is coming out with an await and async keywords which will basically signal to the compiler that method should be re-written to an asynchronous. So for the example you gave, if the await keyword was available in coffeescript, you could just write: await console.log(user.referrer.name); And the coffeescript compiler could then re-write callback style.
- roryokane 15y agoIn fact, IcedCoffeeScript (http://maxtaco.github.com/coffee-script/ http://maxtaco.github.com/coffee-script/) already implements this, using keywords called "await" and "defer".
- roryokane 15y agoYes, there is already a CoffeeScript fork that makes asynchronous code less cumbersome by making it look like synchronous code. It's called IcedCoffeeScript and is at http://maxtaco.github.com/coffee-script/ http://maxtaco.github.com/coffee-script/.
- olegp 15y agoYou can get the best of both worlds: a synchronous programming model combined with the low memory memory usage of Node by using fibers. The trust issues relating to cooperative multitasking that you mention are still valid, but I haven't run into these problems in practice. There are some benchmarks in the README to Common Node, a library that I wrote that makes using fibers easier: https://github.com/olegp/common-node#readme https://github.com/olegp/common-node#readme Lazy loading becomes very straightforward and something I do fairly often when using e.g. https://github.com/olegp/mongo-sync https://github.com/olegp/mongo-sync