5

I have started reading node.js. I have a few questions:

  1. Is node better than multi-threading just because it saves us from caring about deadlocks and reduces thread creation overhead, or are there are other factors too? Node does use threads internally, so we can't say that it saves thread creation overhead, just that it is managed internally.

  2. Why do we say that node is not good for multi-core processors? It creates threads internally, so it must be getting benefits of multi-core. Why do we say it is not good for CPU intensive applications? We can always fork new processes for CPU intensive tasks.

  3. Are only functions with callback dispatched as threads or there are other cases too?

  4. Non-blocking I/O can be achieved using threads too. A main thread may be always ready to receive new requests. So what is the benefit?

karel
  • 3,880
  • 31
  • 37
  • 42
  • There are a bunch of similar thread which discuss this topic, i.e. http://stackoverflow.com/questions/5062614/how-to-decide-when-to-use-node-js – SGD Apr 02 '15 at 19:15
  • For a historical perspective on the issue, see: http://stackoverflow.com/questions/3759683/how-node-js-server-is-better-than-thread-based-server/3759991#3759991 – slebetman Apr 06 '15 at 02:17
  • Also see the original c10k problem that started it all: http://www.kegel.com/c10k.html – slebetman Apr 06 '15 at 02:18

1 Answers1

0
  1. Correct.

  2. Node.js does scale with cores, through child processes, clusters, among other things.

  3. Callbacks are just a common convention developers use to implement asynchronous methods. There is no technical reason why you have to include them. You could, for example, have all your async methods use promises instead.

  4. Everything node does could be accomplished with threads, but there is less code/overhead involved with node.js's asynchronous IO than there is with multi-threaded code. You do not, for example, need to create an instance of thread or runnable every time like you would in Java.

user886596
  • 2,100
  • 3
  • 28
  • 51
  • 1
    1 is partly correct. Node.js does not use threads internally. It's a giant while() loop around select()/poll()/epoll()/libevent – slebetman Apr 06 '15 at 02:15