Node Fundamentals

BFF architecture background for front

SSR server-side rendering, why do server-side rendering, improve SEO, and reduce http requests, the problem of slow first screen loading

How to deploy node and how to connect with nginx, how about security

vue + node

1. Analysis of the principle of asynchronous IO

a. The benefits of asynchronous IO

Front-end can eliminate UI blockage through asynchronous IO

IO is expensive, async IO is more expensive, doesn't that slow things down?

NodeJS is for IO-intensive, not CPU-intensive, Libuv does polling, asynchronous memory leaks

Libuv is the internal event polling mechanism of nodejs, used to support windows and Linux

After the node receives the request, it puts the request into libuv, and libuv turns on the event loop (event loop). The polling enables different mechanisms according to different systems. Linux is Thread pool, windows is IOCP, and the callback function is executed after the event is completed, which will data is pushed to the foreground,

Difference between cluster and fork

cluster is master

fork is a copy strategy

Basic knowledge:
CPU clock cycle, 1/cpu main frequency -> 1s/2.7 GHz, the clock cycle refers to the calculation time interval

2. Several special APIs

a. setTimeout and setInterval do not participate in the thread pool

b. process.nextTick() is implemented similarly,

c. setImmediate() has lower priority than process.nextTick()

d. How Node implements a sleep

async function test(){

  console.log('hello');

  await sleep(1000);

  console.log('world');

}

function sleep(ms){

  return new Promise(resolve => setTimeout(resolve,ms))

}

test();

 

3. Application of functional programming in Node

Higher-order functions, which take functions as input or return values, form a way of accepting results in a subsequent pass-through style, rather than a single return value form. A pass-behind-style program passes the business focus of the function from the return value into the callback function.

app.use(function(){})

var emitter = new events.EventEmitter;

emitter.on('', function(){})

Partial function (currying), the form of specifying some parameters to generate a new custom function is a partial function.

is to generate a function, and then return a function

var currying = function (fn) {
     // fn refers to the official's method of digesting his wife 
    var args = [].slice.call(arguments, 1 );
     // args refers to the legal wife 
    return  function () {
         // already have 
        var newArgs = args.concat([].slice.call(arguments));
         // These wives use fn to digest and utilize to complete the feat of Senior Wei Xiaobao and return 
        return fn.apply( null , newArgs);
    };
};

4. Commonly used node control asynchronous technology

step、wind、bigpipe、Q.js

async await

Promise/defferred is a way to execute asynchronous calls first and delay delivery. Promises are the high-level interface, events are the low-level interface. The low-level interface can build more complex scenarios. Once the high-level interface is defined, it is not easy to change, and it no longer has the flexibility of the low-level interface, but it is very effective for solving problems.

https://www.cnblogs.com/kazetotori/p/6043983.html

The order in which methods are used async/await promise event callback function

Process An application is a process, and a process can have multiple threads

A program contains multiple coroutines, just like a process contains multiple threads. Multiple threads are relatively independent, have their own context, and the switching is controlled by the system; while the coroutine is also relatively independent and has its own context, but its switching is controlled by itself, and switching from the current coroutine to other coroutines is controlled by the current coroutine .

https://www.cnblogs.com/kex1n/p/7425972.html

http://cnodejs.org/topic/58ddd7a303d476b42d34c911

In my understanding of coroutines, coroutines are included in threads, and a thread contains multiple coroutines, that is, an asynchronous operation using promises will generate a coroutine.

5. Node memory management and optimization

a. v8 garbage collection mechanism

http://huang-jerryc.com/2016/04/14/NodeJS%E4%B8%AD%E8%A2%AB%E5%BF%BD%E7%95%A5%E7%9A%84%E5%86%85%E5%AD%98/

The memory usage of node is limited, which mainly depends on the source of v8. The 64-bit system can use 1.4G, and the 32-bit system can use 0.7G. Of course this is also configurable

Pass parameters when node starts

node --max-nex-space-size=1024 app.js // The unit is KB
node --max-old-space-size=2000 app.js // The unit is MB
 
The node adopts a generational garbage collection mechanism, which is divided into the new generation and the old generation.
The new generation stores the newly defined variables and uses the Scavenge algorithm. The algorithm process:
In the scavenge algorithm, the new generation is divided into two parts, each part is called semispace (half space), only one of the two semispaces is in use, from space and to space, from is in use, and to is in idle state. After the variable is defined, it will be stored in the from space. When garbage collection is performed, the surviving objects in the from space will be checked. These surviving objects will be copied to the to space. If they are not used, they will be released, and then the to and from will be exchanged. In other words, the garbage collection of the new generation is to copy the surviving objects between the two semispaces.
 
The old generation stores variables that have been used all the time. When the new generation is garbage collected, that is, when the variable is copied from from to to, there are two cases in which it will be directly placed in the old generation: 1. If the share of to exceeds 25%, the variable will be directly promoted to the old generation, 2. If the variable has undergone garbage collection, it is directly promoted to the old generation. In this way, there will be less to space, and to and from will be different when exchanged.
The garbage collection mechanism of the old generation should be incremental now (or do you need to configure it yourself?)
Currently, garbage collection is performed by combining the mark-sleep algorithm and the mark-compact algorithm. That is, after marking and sorting, move the living object to one end to clear the unused space, so that the memory is not fragmented, and large data can be stored. How are these two algorithms combined? Since mark-compact needs to move objects, it will affect the speed. Under normal circumstances, the mark-sleep algorithm is used, only when the remaining space is not enough to allocate objects from the new generation. Perform mark-compact algorithm
 
Since node is single-threaded, garbage collection will block the progress of the code, so there is an incremental recycling mechanism, that is, starting from the mark, changing the operation that was previously marked in one go to incremental marking, and splitting it into many small steps , each time a step is completed, let the program execute for a while, and the garbage collection alternates with the application until the marking phase is completed. As for the process of sorting and cleaning, it takes the most time to mark.
 
V8 later introduced lazy sweeping and incremental compaction, making cleaning and sorting actions incremental. At the same time, it is also planned to introduce parallel marking and parallel cleaning to further take advantage of multi-core performance to reduce the time of each pause
 
Memory leaking crawler
b. Common memory leaks
infinitely growing array
Unlimited set properties and values
Private variables and methods in any module are permanently resident in memory, and modules that are not used need to be set to null
Large cycle, no GC chance 
 
6. Application of node cluster
a. Ready to go online
b. pm2 is a process manager for node applications with load balancing 
server cluster
 
 
UV(unique visitor)
Multiple visits of a visitor (identified as the same person through authentication) in one day are counted only once
IP
The same ip is counted only once in a day
PV(page visitor)
Open the page to calculate once
VV(Visit View)
The number of visits to a website is counted as opening the website once, and browsing the page is not counted.
 
 
 
 
 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324463170&siteId=291194637