Reputation: 665
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).
Upvotes: 0
Views: 1126
Reputation: 25446
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)
Upvotes: 2
Reputation: 887225
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.
Upvotes: 0