Why buffer was introduced in Node.js
A buffer
Buffer in English is the meaning of buffer, from which we can initially learn that buffer is introduced to do buffer on the Node server.
In what scenarios does the Node server need buffering
Since Node is a server, you need to handle network requests (NET, HTTP), manipulate file systems (FS, readline), handle images, file uploads and downloads, and so on. Node selects JS and requires a V8 engine to execute JS. Isn’t V8 powerful enough to do the job?
The V8 limit
Why is V8 limited? V8 was created for Chrome, the browser for rendering web pages; For web presentation, V8 was designed to use about 1.4 gigabytes of memory for 64-bit operating systems and 0.7 gigabytes for 32-bit operating systems, which is adequate for web presentation, but not enough for server-side use.
Node allocates memory in C++
Since V8 limits memory usage, node uses C++ to allocate memory, and can get more memory, but Lao tzu says: dao dao, dao dao, name can name, name can name; Similarly, allocated memory size, there will be occupied, blocked, and then slow service, or even crash.
How does the buffer handle the problem in the above scenarios
Mars immigrants
Imagine this scenario, we need to transfer 1 billion organisms (English characters, Chinese characters, Arabic characters, etc.) from The Earth (Disk C) to Mars (disk D), we have built a space station (memory) between the Earth and Mars that can hold 10 million organisms. It would be a disaster if a billion creatures entered the space station at the same time. The Earth’s spacecraft (buffer) has 1000 places (bytes) to carry life to the space station; And then the spacecraft that would go to Mars would have 1,000 places (bytes), and then carry life to Mars.
Concept extraction
Buffer:
Used to represent a fixed – length sequence of bytes, such as a spaceship with a fixed 1000 positionsUTF-8:
It’s a variable length code. It can use 1 to 4 bytes to represent a symbol, varying the length of the byte depending on the symbol. For example: the ship will specify English creatures to occupy 1 position, Chinese creatures to occupy 2 or 3, 4 positions