0

I am very new to Javascript and NodeJS. I was running a simple helloworld programs as follows

Program 1

const durationInSeconds = 10;

console.log('Hello World');

setTimeout(() => {
  console.log(`Program has been running for ${durationInSeconds} seconds.`);
}, durationInSeconds * 1000);

When I ran the program , I was monitoring the processes using the htop command in linux. I noticed that the application was creating 7 node instances of the same application. Why is this happening? Why is it not creating only one node instance for a single simple application? I had this question because if I run a similar program in python I see only one instance of the python application running.

Joe Race
  • 178
  • 10
  • Not entirely. If the node application was doing some semblance of work I may understand. But this is just a simple Hello world with a sleep operation. Why would it need 7 instances running in parallel. I went through the other post and I noticed it has 4 instances. But mine has 7. – Joe Race May 08 '23 at 14:44
  • I edited the question with the screenshot – Joe Race May 08 '23 at 14:48
  • 3
    node doesn't introspect your program's complexity before it starts up its thread pool. – erik258 May 08 '23 at 15:10
  • Adding to @erik258's comment, Node is an interpreted language so Node will run the program line by line and it needs to be ready for anything you throw at it. Compiled languages can optimize the binary because it has "seen" all the code and "knows" what will be needed. – Bharat D Bhadresha May 09 '23 at 01:37
  • 2
    Okay. Makes sense. I delved a bit more into the other post @eskew shared and some of the comments there supplements this answer. Thanks. I shall accept this answer. – Joe Race May 09 '23 at 06:15

1 Answers1

2

Nodejs is needs threads to do other tasks that are handled automatically for your by the V8 engine. Some of the things are

  • Interpreter
  • Event loop
  • Garbage collector
  • Blocking I/O executor and others...

Nodejs make programming easy by hiding these complexities from the programmer. If you need more control on these lower level 'stuff' then you can use C,C++ or other low level lanuages where you have to decide what should in which thread.