suzukimilanpaak
suzukimilanpaak

Reputation: 808

Node.js - Which functions of JavaScript block a process exactly?

I'm trying to understand the architecture of Event loop in Node.js. I came across side by side comparison between a server with setTimeout() and one with sleep() by while clause. setTimeout() was handled asynchronously but sleep() wasn't. http://www.atmarkit.co.jp/fcoding/articles/websocket/01/websocket01a.html (written in Japanese)

I understood this somewhat. But, I came up with a question, 'How can I find which blocks a process and which doesn't by reading source'. How do you determine it?

Upvotes: 0

Views: 251

Answers (1)

Andrey Sidorov
Andrey Sidorov

Reputation: 25446

  1. look at function signature: if it is var result = calculateData(parameters) than it block (btw every function in node block io processing loop, but most functions just queue io requests and exit)
  2. measure function execution time, 'non-blocking' function execution time should be small compared to time until result callback is called.
 var start = new Date();
 doesItBlock(function(err, result) {
     console.log('doesItBlock callback called after ' + (new Date - start));
 });
 console.log('doesItBlock exited after ' + (new Date - start));

Upvotes: 1

Related Questions