Chapter one: Introduction to Node

Chrome has a WebKit layout engine in addition to V8 as a JavaScript engine.

2. The weakness of single threading is threefold

(1) Unable to utilize multi-core CPU.

(2) Errors will cause the entire application to quit, and the robustness of the application is worth testing.

(3) A large number of computations occupy the CPU so that asynchronous I/O cannot be called.

3. In the browser, Web Workers can create worker threads to perform calculations, so as to solve the problem of large JavaScript calculations blocking UI rendering. In order not to block the main thread, the worker thread passes the result through message passing, which also prevents the worker thread from accessing the UI in the main thread.

4. Node uses the same approach as Web Workers to solve the problem of large computations in a single thread: child_process. The presence of child processes means that Node can handle the robustness and inability of single threads to take advantage of multi-core cpus in stride. By distributing computations to child processes, you can break down a large number of computations and then pass the results through inter-process event messages, which keeps the application model simple and low dependency.

5. Node is network oriented and good at parallel I/O, which can effectively organize more hardware resources to provide more good services.

6, appropriate adjustment and decomposition of large operation tasks into multiple small tasks, so that the operation can be properly released, do not block the initiation of I/O calls, so that you can enjoy the benefits of parallel asynchronous I/O at the same time, and can make full use of CPU.

7. For long operations that take longer than normal blocking I/O, the usage scenario needs to be re-evaluated

8. Node does not provide multithreading for computing, but there are two ways to take full advantage of multi-core cpus:

(1)Node can make more efficient use of multi-core CPUS by writing C/C++ extensions, which can be implemented in C/C++ where V8 cannot achieve extreme performance.

(2) If single-threaded Node does not meet the requirements, even after using C/C++ extension is still not enough, then through the way of child process, part of the Node process as a resident server process for calculation, and then use multi-process message to pass the results, the calculation and I/O separation, so as to make full use of the CPU.

Chapter two, module mechanism

1. Just as browsers cache static step files to improve performance, Node caches all imported modules to reduce the overhead of secondary references. The difference is that the browser only caches files, whereas Node caches objects after compilation and execution.

2. In fact, Node wraps the header and tail of the retrieved JavaScript file during compilation. Added (function(exports, require, _filename, _dirname){\n to the header and \n} to the tail; . A normal JavaScript file would be wrapped like this:

(function(exports, require, _filename, _dirname){
  var math = require('math');
  exports.area = function(radius){
    return Math.PI * radius * radius;
  };
});
Copy the code

This is why js contains require, exports and module variables

Chapter 3. Why Asynchronous I/O

1. JavaScript executes on a single thread in the browser, and it shares a thread with UI rendering. This means that the UI rendering and response is stagnant while the JavaScript is executing, and if the execution time of the step is more than 100 milliseconds, the user will feel the page is stuck and think the page has stopped responding.

2. Synchronizing I/ OS takes M+N+… , asynchronous is Max (M,N…) . As websites or applications proliferate, data will be distributed across multiple servers, and distribution will be the norm. Distributed also means that the values of M and N increase linearly, which magnifies the difference in performance between asynchronous and synchronous. I/O is expensive, distributed I/O is even more expensive, and the front-end experience is better when the back end can respond quickly to resources.

3. The single-line simultaneous programming model will block I/O and result in the poor use of hardware resources. Multithreaded programming model is also a headache for developers because of deadlock, state synchronization and other problems in programming. Node takes advantage of single threads to avoid multithreaded deadlock and state synchronization issues. Use asynchronous I/O to keep single threads out of blocks for better CPU usage.

4. To compensate for the inability of a single thread to take advantage of a multi-core CPU, Node provides a child process similar to Web Workers in a browser that uses CPU and I/O efficiently through worker processes.

5, blocking I/O causes CPU wait waste, non-blocking trouble is the need to polling to confirm whether the data acquisition is completed, it will let the CPU processing state judgment, is a waste of CPU resources.

6. We often refer to Node as single-threaded, which is simply JavaScript execution in a single thread. In Node, both the * NIx and Window platforms have separate thread pools for I/O operations.

7. Node also has some ASYNCHRONOUS apis that have nothing to do with I/O, such as setTimeout(),setInterval(), setImmediate (), and process.nexttick ().

SetTimeout () and setInterval() are implemented in a similar way to asynchronous I/O, but without the participation of an I/O thread pool. The timer created by calling setTimeout() or setInterval() is inserted into a red-black tree inside the timer observer. Each Tick, the timer object is iterated out of the red-black tree to check whether the timer time is exceeded. If the timer time is exceeded, an event is formed and its callback function is executed immediately.

9. The use of timers requires the use of red-black trees, the creation of timer objects and iteration operations, while setTimeout(fn, 0) is a waste of performance. In fact, the process.nexttick () method is relatively lightweight in that it only queues the callback function and has a low time complexity.

10. The callback function in process.nexttick () has a higher priority than setTmmediate(). Process.nexttick () is the idle observer, and setImmediate() is the check mediate. In each cycle, idle observer precedes I/O observer, and I/O observer precedes check observer.

In particular, the process.nexttick () callback is stored in an array, and setImmediate() is stored in a linked list. Behaviorally, ProcessA.Nexttick () executes all the callbacks in the array in each loop, while setImmediate() executes one of the list’s callbacks in each loop.

Event loops are at the heart of asynchronous implementations.

Chapter 4, asynchronous programming