The Node profiles
JavaScript works on both the browser and server side, the main difference being that JavaScript running on the server side does not have DOM and BOM operations
1. The characteristics of the Node
- Single-threaded, Node maintains the single-threaded nature of JavaScript
- Asynchronous I/O, using JavaScript eventLoop
- Events and callbacks are efficient ways to handle asynchrony
- Cross-platform because of the libuv intermediate layer between Windows and Node.js, which makes it possible for Node to run on Windows and other platforms
Node introduces different modules during development, including core modules and third-party modules.
2. Module classification
(1) Core module
- C/C++ built-in modules are low-level modules that can be precompiled into.node files and then loaded and executed by calling process.dlopen(), which need to be called by JavaScript core modules.
- Javascript core modules, which refer to modules such as FS, OS, and HTTP, are second in priority to cache loading and are compiled into binary code during source compilation of Node, making them the fastest to load. Its main role is to serve as a hub between built-in modules and custom modules, or simply pure functionality.
(2) Custom modules
Is a special file mode, which may be in the form of a file or package. This type of module lookup is the most time-consuming and the slowest of all searches. Its query rules are:
- Node_modules directory in the current file directory;
- Node_modules directory in the parent directory;
- Node_modules directory in the parent directory;
- Recurse up the path to the node_modules directory in the root directory.
It is generated in a very similar way to JavaScript’s prototype or scope chain lookup. It can be known that the deeper the file path is, the more time it takes to find modules, which is why the loading speed of custom modules is slow.
Note: The front and back ends are common modules. JavaScript on the browser side needs to be distributed to multiple clients for execution from the same server, while JavaScript on the server side requires the same code to be executed for many times. The bottleneck of the former lies in bandwidth, while the bottleneck of the latter lies in CPU and memory resources.
Note: CommonJs is used for node module loading. The API used is require, exports and Module.
3. The asynchronous I/O
The single-line synchronized programming mode blocks I/O and results in less optimal use of hardware resources. Multithreaded programming also makes development difficult due to deadlocks, state synchronization and other issues in programming. Node balances the optimal solution between the two and uses single thread to avoid multi-thread deadlock, state synchronization and other problems. Use asynchronous I/O to keep single threads out of blocks for better CPU utilization. Node.js timers need to be noted here:
- setTimeout
- setInterval
- setImmediate
- process.nextTick
4. The V8 memory is faulty
By default, memory in V8 can only be used for about 1.4GB on 64-bit systems and 0.7GB on 32-bit systems if it is always allocated. The new generation is only a fraction of that, with 16MB on 64-bit systems and 8MB on 32-bit systems.
- New generation, the new generation is stored in the short life of the object, by the garbage collector recycling
The Cenozoic insane are processed using the Scavenge algorithm, which is the splitting of the Cenozoic space into two sections, one half object and the other free. Newly added objects are stored in the object area, and when the object area is nearly full, a garbage collection operation is performed. The sub-garbage collector copies the inventory objects into the free area and arranges them in an orderly manner, a copying process that is also equivalent to a memory decluttering operation.
After the copy is completed, the object area and the idle area can be reversed. The role reversal operation can also make the two areas reused indefinitely in the new generation.
Because the newborn area is small, it is easy to fill the entire area with living objects. To solve this problem, the JavaScript engine adopts the object promotion strategy, where objects that have survived two garbage collections are moved to the old area.
- Old generation, old generation objects that have lived for a long time and are collected by the main garbage collector
Mark-compact is an extension of mark-sweep. Mark-sweep is traversing the call stack, marking variables that are not referenced, then marking them, and then clearing the marked data in a subsequent garbage sweep. Mark-tidy, on the other hand, does not clean up the reclaimable objects directly, but moves the objects in stock to one end, and then cleans up the memory beyond the end boundary.
5. The buffer object
Buffer is a typical JavaScript/C++ module that implements performance related parts in C++ and non-performance related parts in JavaScript. V8’s memory problems were briefly described above, but instead of being in V8’s heap memory, the buffer is allocated at the C++ level of Node. Because processing a large number of bytes of data can not be used to require a little memory to the operating system to apply for a little memory, this will cause a large number of memory application system calls, the operating system has a certain pressure. For this reason, Node adopts the strategy of applying for memory in C++ and allocating it in JavaScript. According to 8KB as the limit, Buffer is divided into:
- Small objects
- The big object
Reference Book: NODEJS in Plain English (Park Ling)