Special note: this article is unwritten, read carefully.

The front end of the interview summary “of point, line and plane series is to curl of mixed and disorderly the front end of the domain knowledge, by the point and line, more important, the aim is to help the masses of the front-end students review consolidate or leak fill a vacancy or add and delete, in order to set up the front domain knowledge system, in order to better to be prepared for the front end of the interview, and do a qualified, the progress of the front-end development engineers.

  • NodeJS
  • V8 memory management model
    • Event loop
      • Event Loop
      • Principle of event loop
      • Process. NextTick 🆚 setImmediate
  • Process the Process
    • Cluster
  • Koa

NodeJS

V8 memory management model

All memory occupied by this process while the Node program is running is called resident memory:

  1. Code area: Stores code snippets to be executed
  2. Stack: Holds local variables
  3. Heap: Holds objects, closure contexts
  4. Out-of-heap memory: allocated by c++, not managed by V8, and not reclaimed by V8. (Buffer data)

V8 garbage collection ♻️

  1. New generation:
    • Divide the memory evenly into two parts, the used space is called FROM and the idle space is called TO.
    • Allocates live objects TO the TO space, and then clears the FROM space
    • Swap FROM and TO Spaces TO continue memory allocation
    • The new generation promotes the old generation
      • Objects that survive multiple times are promoted to old age
      • To Space Memory usage exceeds 25%
  2. The old generation:
    • Flag to clear mark-sweep
    • Mark-compact (Tidy contiguous memory)

Event loop

High concurrency: single-threaded non-blocking asynchronous IO (main thread event loop mechanism and implementation of underlying thread pool)

Event Loop

Principle of event loop

  1. The node initialization

    • Initialize the Node environment
    • Execute input code
    • Execute the process.nextTick callback
    • Perform microtasks
  2. Enter the Event Loop

    1. Timers phase
      • Check whether the timer queue has expired timer (setTimeout/setInterval) callbacks. If so, execute the expired callbacks in ascending timerId order.
    2. IO callback stage
      • Check for PENDING IO callbacks, and execute them if any
    3. Idle, prepare
      • Nodejs internal call
    4. Poll phase
      • Check if there are any outstanding callbacks, and if so, in one of two cases
        1. If the queue is not empty (including expired timers and IO events), the available callback is executed.
        2. If the queue is empty, check for setImmediate callbacks,
          • If yes, exit the poll phase and enter the check phase.
          • If not, node blocks here before timeout, waiting for a new event notification.
      • If there are no outstanding callbacks, exit the poll phase
    5. The check phase
      • If there is a setImmediate callback, perform the callback
    6. closing callback
      • If a socket or handler is suddenly closed (such as socket.destroy()), the ‘close’ event is emitted at this stage.

    Before each subphase of the event loop exits, it executes:

    1. Check if process.nextTick exists and execute if so
    2. Perform microtasks
    3. Exit the current phase
  3. Check whether there are active Handlers (timers, IO event handlers)

    • If so, proceed to the next cycle
    • If not, exit the event loop and exit the program

Process. NextTick 🆚 setImmediate

Process. nextTick is a timer for Nodejs, which is executed in this cycle and is the fastest of all asynchronous tasks. After Node completes all synchronization tasks, it executes the process.nextTick task queue.

Process. nextTick does not belong to any stage in the Event Loop, but the tick inserted in nextTickQueue is executed directly before each stage of the Event Loop ends until the entire Queue is processed.

SetImmediate adds events to the end of the current task queue, meaning that the event it specifies is always executed in the next Eventloop.

The recursive call to Process. nextTick causes IO starvation. SetImmediate is recommended.

Example:

Process the Process

  1. Check the process

    • ps -ef
  2. The start directory of the current process

    • process.cwd()
  3. Change working directory

    • process.chdir()
  4. The standard flow

    • process.stdin
    • process.stdout
    • process.stderr
  5. What is the difference between child_process.fork and POSIX fork? Nodejs’s child_process.fork() implementation on UNIX ends up calling POSIX’s fork, but POSIX needs to manually manage the child’s resource release. Child_process.fork doesn’t have to worry about this. Nodejs is automatically released, and you can choose from option whether to allow child processes to survive the death of the parent process

    • spawnStart a child process to execute a command
    • execStart a child process with callback parameters to get information about the child process, you can specify the timeout period for the process to run.
    • forkSpawn enhanced version, which returns a child_process object and can interact with child processes.
  6. Child. Kill with the child. Send

    • One is based on signal system and one is based on IPC.
  7. Does the death of a parent or child affect each other? What is the orphan process?

    • The death of the child does not affect the parent process, but when the child dies (when the last thread in a thread group, usually the “lead” thread dies), it sends a death signal to its parent.
    • If the parent process dies, the subprocess usually dies with it.
      • But if the child is runnable, dead, and so on, it will be adopted by process 1 (the init process), thus becoming an orphan process.
      • In addition, when the child dies (in the “terminated state”) and the parent does not call wait() or waitpid() in time to return information about the dead process, the child still has a PCB left in the process table, which is called zombie process.

Cluster

Cluster is a common multi-core nodeJS method. It is implemented using child_process.fork, so the processes generated by Cluster communicate with each other through IPC, and there is no room for copying the parent process. Instead, the parent and child processes are separated by joining cluster.isMaster.

const cluster = require('cluster')
const http = require('http')
const numCPUs = require('os').cpus().length

if(cluster.ismaster) {// Only the parent process executes // fork workersfor(i = 0; i<numCPUs; i++) { cluster.fork() } cluster.on('exit', (worker) => {
      console.log(`worker ${worker.process.pid} died`)
   })
} else// Workers can share any TCP connection http.createserver ((req, res) => {res.writehead (200) res.end('hello world')
   }).listen(3000)
}
Copy the code

Koa

Compose:

function compose(middlewares){
   return ctx => {
      const dispatch = i => {
         const middleware = middlewares[i]
         if(! middleware)return
         return middleware(ctx, () => dispatch(i+1))
      }
      return dispatch(0)
   }
}
Copy the code

Context: ctx

class Context{
   constructor(req, res) {
      this.req=req
      this.res=res
   }
}
Copy the code

Koa-mini

class Application() {
   constructor() { this.middlewares = [] } listen(... args) { const server = http.createServer(async (req,res) => { const ctx = new Context(req,res) const fn = compose(this.middlewares) await fn(ctx) ctx.res.end(ctx.body) }) server.listen(... args) } use(middleware){ this.middlewares.push(middleware) } }Copy the code

I am untalented and uneducated, the article unavoidably has the wrong place, but also hopes the classmate criticizes corrects, grateful endless!

GitHub Repo