The author asked 10 questions in a tech share, but many developers in the room couldn’t give the exact answers. Do you understand all the 10 questions?

What is the call stack, and is it part of V8?

The call stack is part of V8. The call stack in V8 is a data structure that tracks method calls. Each time a function is called, V8 references that function in the call stack. And this happens every time you call any other function (including recursively called functions).

When the function call ends V8 pops the function and returns its value at the function call.

This is important to understand because each Node process has only one call stack, and if your stack is busy, your Node process is busy.

What is an event loop? Is he part of V8?

Which module is the event loop in the following figure?

The event loop is provided by the Libuv library, which is not part of V8.

The Event loop is used to process external events and convert them to callbacks, which select events from the event queue and push their event callbacks to the call stack.

The event loop is in the middle of the photo and acts like an organizer. When the V8 call stack is empty, the event loop can decide what to do next.

What does Node do when the call stack and event loop are empty?

It just exits.

When you run the Node program, Node automatically starts the event loop, and when the event loop is idle and there is no other action, the process exits. To keep the Node process running, you need to put something in an event queue. For example, when you start a timer or AN HTTP server, you tell the event loop to continue running and check for these events.

Besides V8 and Libuv, what external dependencies do Node have?

Here are the dependent libraries used by the Node process:

  • http-parser
  • c-ares
  • OpenSSL
  • zlib

They are all dependent libraries used by Node, they have their own source code, protocol. Node just uses them. So if you’re doing data compression and you get an error, don’t blame Node.

Can YOU run Node without V8?

This can be a tricky one. You’ll need a VM to run the Node process, but V8 isn’t the only VM you can use. You can also use Chakra.

Module. exports what is the difference between exports?

You can use the module.exports API, of course you can also use exports.

module.exports.g = .  // Ok
exports.g = .         // Ok
module.exports = .    // Ok
exports = .           // Not Ok
Copy the code

Export is just a reference or alias of module.exports. Exports and module.exports are no longer related if exports are reassigned.

Why are top-level variables defined not global?

If you have module1 defining the top-level variable g:

// module1.js
var g = 42;
Copy the code

If you require module1 in module2 and try to access the variable G, you’ll get g is not defined.

But if you do the same thing in a browser, you can access all of scirpt’s top-level variables (already defined),

Each Node file has its own IIFE (just-in-time function expression). All variables declared in Node files are limited to IIFE.

Related question: What does running the following Node file output?

// script.js
console.log(arguments);
Copy the code

You will see some parameters:

Because Node executes a function. Node wraps your code in a function that explicitly defines the above five parameters.

Exports, require, and Module objects are all globally available in each file, but are different in each file. How is this done?

When you need to use the Require object, you simply use it as if it were a global variable. However, if you examine them in two different files, you will see two different objects. How did that happen?

Also because of IIFE:

As shown above, IIFF passes your code the following five arguments: exports, require, Module, __filename and __dirname. When you use them in Node, these five variables appear to be global variables, but they are really just function arguments.

What are module cycle dependencies:

What happens if you have a module1, module1 requires module2 and module2 requires Module1 in reverse? Will it report an error?

// module1
require('./module2');
Copy the code

// module2
require('./module1');
Copy the code

No errors will be reported. Node allows this operation.

So module1 requires Module2, but because Module2 requires Module1 (module1 is not complete at this point), module1 will only get a partial version of Module2.

This will be warned.

When to use file system *Sync (e.g. ReadFileSync)

Each FS method in Node has a synchronous version. Why use synchronous rather than asynchronous methods?

Sometimes using a synchronous approach is a good option. For example, it can be used during initialization while the server is still loading. Using synchronous methods is acceptable, as long as you use synchronous methods for one-time things (multiple callbacks like Http requests are definitely wrong).

The original:
You don ‘t know the Node





Recommended reading:
A new V8 is coming, and Node.js performance is changing