Node.js ~ Basics

1. node asynchronous non blocking I/O

Synchronization and asynchrony refer to the characteristics of certain methods and calls. Synchronization is when a function call is sent, the caller will not return until the result is obtained. Asynchrony is that the caller can not get the result immediately after the call is issued, but when the asynchronous function is completed, the caller is notified by state, notification or callback.
Blocking and non blocking: describes the state of the process and the five states of the process: create, ready, run, block and terminate. Therefore, when we talk about blocking and non blocking, we must refer to a process. When a process initiates a call, if the process changes from running state to blocking state, it means that it is a blocking call, and vice versa.

For example, the story of Uncle Wang and the kettle

Blocking and non blocking indicate whether Uncle Wang (the main process) is waiting. Synchronous and non synchronous indicate the calling posture of the kettle (whether to actively send a notice that the water is boiling).

The main process is blocked: if Uncle Wang waits for the water to boil and does nothing else,
The main process is non blocking: if Uncle Wang doesn't wait for the water to boil, he can do other things
Synchronization: the kettle will not give notice after the water is boiled
Asynchronous: after the water boils, the kettle automatically rings to give a notice.

const fs = require('fs')
// synchronization
// const data = fs.readFileSync('./download.js')
// console.log(data,data.toString())

// asynchronous
fs.readFile('./download.js',(err,data) => {
    if(err) throw err
    // Binary stream
    console.log('//',data)
    // buffer conversion
    const bufferInfo = Buffer.from(data).toString('utf-8')
    console.log('bufferInfo', bufferInfo)
})

2,Buffer
brief introduction

Buffer: It is a physical mapping of processing data temporarily stored in memory in binary form

early stage js There is no mechanism to read binary operations, js Originally designed for operation html

buffer

Buffer: similar to bus ride, the speed and time of passengers' arrival are beyond our control, and the only thing we can control is when to start. The reading and processing of so data in the flow, the speed of data reaching and the speed of process consumption will make the early arriving data in the waiting area.

Buffer memory mechanism: pre application, post allocation, and the memory occupied by buffer is no longer allocated by V8, but in node JS C + + level to complete the application and allocate memory in JavaScript. Therefore, this part of memory is called out of heap memory.

Buffer memory allocation summary
(1) An 8KB memory space will be initialized during the initial load, buffer JS source code is reflected
(2) According to the requested memory size, it is divided into small Buffer objects and large Buffer objects
(3) In the case of small Buffer, it will continue to judge whether the slab space is sufficient. If the space is sufficient, it will use the remaining space and update the slab allocation status. The offset will increase. If the space is insufficient and the slab space is insufficient, it will create a new slab space for allocation
(4) In case of large Buffer, the createUnsafeBuffer(size) function will be used directly
(5) Whether it is a small Buffer object or a large Buffer object, the memory allocation is completed at the C + + level, and the memory management is at the JavaScript level. Finally, it can be recycled by the V8 garbage collection tag.
Common applications of Buffer:
Create buffer: you can use buffer from(),Buffer.alloc() and buffer Allocunsafe() can be created in three ways
Merge buffer: buffer concat()

// Create a Buffer with a length of 10 bytes filled with 0
// alloc: allocate = > the initialized Buffer, which can ensure that the newly created Buffer will never contain old data
// Create a buffer with a size of 10 bytes
const buf1 = Buffer.alloc(10)
 console.log(buf1)

// Create a Buffer containing ascii
const buf2 = Buffer.from('a')
console.log(buf2,buf2.toString())

// The created Buffer contains UTF-8 bytes
const buf3 = Buffer.from('chinese')
console.log(buf3)

// Merge Buffer
const buf4 = Buffer.concat([buf2,buf3])
console.log(buf4,buf4.toString())

3,Stream

Stream: it is a conveyor belt and processor for handling data, with direction, status and buffer size (it is an abstraction of input and output devices, which can be files, networks and memory).
In fact, req and rep between http are actually a stream. When there is no res.end, the normal request is actually in memory (in the buffer). Therefore, the file assembly will occupy more memory and introduce the stream. In this way, the memory will not occupy the size of the whole picture, but a bit in the pipeline.
For example:

	// Make a pipe between the file and the browser
 fs.createReadStream('./'+url).pipe(response)

Function: to operate large files, you need to use stream to flow data out and in like a pipeline

//Binary friendly, picture operation
const fs = require('fs')
const rs = fs.createReadStream('./img.png')
const ws = fs.createWriteStream('./img2.png')
rs.pipe(ws)

Keywords: node.js

Added by jamieh on Wed, 05 Jan 2022 07:35:31 +0200