0

I have been reading quite a bit about blocking and non-blocking operations in node.js, and so far this is what I've put together:

database operations --> non-blocking;
open/close files -----> non-blocking;
network operations ---> non-blocking;

My big doubt is about "normal" operations, that is, those operations which do not require access to anything more than the RAM. In fact, let's say I run a long, long, long set of these "normal" operations in my node server. Is there a point at which they are so many that their execution might actually decrease the performance of the server? (obviously, I am just a beginner, so don't be too harsh).

user3289157
  • 645
  • 2
  • 13
  • 24

2 Answers2

2

There is no magic "non-blocking" call in javascript or node.js, so the notion of "blocking" or "sync" function call depend a lot on what is desired effect of a function. All function calls in javascript (with exception for ES6 generators) are synchronous, so I'd define "non blocking" call as follows:

Function often referred as "non-blocking" if at the moment function return control to parent desired side effect may not be ready yet. Instead, some state is changed in the runtime (commands put in the queue, file descriptors added to watch list etc) and runtime may resume control to another function referenced by "non-blocking" function as a parameter ( often called "callback" function)

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
0

Node.js is a single-threaded runtime; it does all CPU-bound work synchronously on that thread.

As long as that thread is running synchronous code (memory access & computations), no other code can run.

All non-blocking operations (network & disk access) run asynchronously, and call back into the thread when results come back.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • so does this mean that if a lot of blocking operations are to take place, executing them in a child process is the way to go? – user3289157 Jul 29 '14 at 03:00
  • 1
    most non-blocking operations happen on the same thread as main javascript ( _not_ on some magic background thread ) – Andrey Sidorov Jul 29 '14 at 04:59
  • 1
    @user3289157: If you have large amounts of blocking work, Node.js is a poor choice. – SLaks Jul 29 '14 at 14:24