There is no meaning in life without suffering, reading for the great rejuvenation of the Chinese nation.

Antecedents to review

The last article focused on a commonJS specification issue, so today we’ll pick up where we left off and talk about nodeJS module lookup

The Node module,

Introducing modules into Node generally goes through the following steps:

  • Path analysis
  • File location
  • Compile implementation

In Node, modules can be divided into two categories. One is that the modules provided by Node become core modules. The other is user-written modules called file modules.

Core modules are compiled into binary executables during Node source compilation. When the Node process starts, the core module is directly loaded into memory, so when the core module is introduced, the file location and compilation execution steps can be ignored, and the path analysis is prioritized, so the core module is the fastest to load.

The file module is dynamically loaded during operation, which requires complete path analysis, file location, compilation and execution process. The loading speed is slower than the core module.

Module loading process

  • Loading from cache is preferred.

Node caches all imported files to reduce the overhead of secondary references. Unlike browser caches, which cache files, Node caches compiled and executed objects.

No matter the core module or the file module, the second load of the same module child adopts the cache first mode. However, the cache check of the core module takes precedence over that of the file module.

Path analysis and file location

Different identifiers, module search and location also have different degrees of difference.

    1. Module identifier analysis
    • Core modules such as HTTP, FS, PATH...
    • Absolute path or relative path
    • Custom module children can also be understood as NPM packages

    The core module is second only to cache loading and is compiled into binary code in the Node source code, so it is the fastest to load.

    Identifiers such as absolute or relative paths are usually our own written file modules. The require() method converts the path to the real path and uses the real path as an index to store the compiled result in the cache for faster download and loading.

    Custom module children can also be understood as NPM packages. This type of module lookup is the slowest, and the process looks like this: Node_modules in the current directory –> node_modules in the parent directory –> Recursively find node_modules in the parent directory –> find node_modules in the root directory. This process is similar to the JS scope chain, but the deeper the file level, the more time the search takes.

    1. File location

    The efficiency of secondary loading is greatly improved when loading from cache first, but there are other details to consider, such as file extensions, directory handling, package handling, and so on

    By default, the identifier in the require() method does not have an extension, in which case Node will try to complement the extension in the order of.js,.json, and.node.

    During this process, the FS module synchronously blocks to determine whether the file exists. Node has some performance issues because it is single-threaded, so if it is a JSON file with an extension, it will improve performance somewhat.

    In the process of analyzing the identifier, require() may not find the corresponding file after parsing the extension, but instead get a directory, which Node treats as a package. It then looks for package.json, parses the description object through json.parse (), and locates the lookup from the file specified by the main property. If there is no extension, the extension is completed. If the entire process is complete and no target file is found, an exception is thrown.

How to compile modules and NPM

conclusion

  • Preferentially load from cache
  • Recursive search

One last word

  1. Move your rich little hands and “like it.”
  2. Move your rich little hands, “click here”
  3. All see here, might as well “add a follow”
  4. Might as well “forward”, good things to remember to share

Click on the public account