Related recommendations: "node js tutorial"
I read Pu Ling's Nine Lights and One Deep NodeJS to get started with Node. , Park Dada’s book talks very little about practice and devotes more space to explaining principles. After listening to so many principles, he later started to use NodeJS in the field of front-end engineering but was hampered everywhere. Summarizing the reasons, he found that the difficult part in NodeJS is nothing more than files and files. Networking, file operations and networking all rely on a very important object - Stream, which is exactly what Park did not mention in his book.
Buffer Park Dada mentioned it in the book, but because the stream is actually processing the Buffer, it still needs to be briefly summarized.
As introduced in the official API, before ES6 introduced TypedArray, JavaScript had no mechanism to read or operate binary data streams. The Buffer class was introduced as part of the NodeJS API to be able to interact with network streams such as TCP and file streams.
Now that TypedArray has been added to ES6, the Buffer class implements the Unit8Array API in a way that is more optimized and suitable for NodeJS operations.
In short, the Buffer class is used to process binary data. Because it is so commonly used, it is placed directly in a global variable, and there is no need to require when using it.
Instances of the Buffer class are similar to integer arrays, but the size of the buffer is determined when it is created and cannot be adjusted. The difference with the Buffer object is that it does not go through V8's memory allocation mechanism. Buffer is a module that combines JavaScript and C. The memory is applied for by C and allocated by JavaScript.
We will not discuss the related knowledge of Buffer memory allocation. Interested students can read Park Laoshi's book.
Before NodeJS v6, Buffer was instantiated by calling the constructor, returning different results according to the parameters. For security reasons, this method has been abolished in versions after v6, and
// 0x 表示 16 进制 Buffer.from([1, 2, 3]) // [0x1, 0x2, 0x3] Buffer.from('test', 'utf-8') // [0x74, 0x65, 0x73, 0x74] Buffer.alloc(5, 1) // [0x1, 0x1, 0x1, 0x1, 0x1] Buffer.allocUnsafe(5); // 值不确定,后面详谈
Buffer.allocUnsafe() will execute faster than
Buffer.alloc() It is unsafe to look at the name, and it is indeed unsafe.
Buffer.allocUnsafe() is called, the allocated memory segment has not been initialized (not returned to zero), so the memory allocation speed is very slow, but the allocated memory segment may contain old data. If you do not overwrite these old data when using it, it may cause memory leaks. Although it is fast, try to avoid using it.
Buffer.from(string [, encoding])
buf.toString([encoding[, start[, end]]])
Buffer.concat(list[, totalLength])
const buf = Buffer.from('中文字符串!'); for(let i = 0; i < buf.length; i+=5){ var b = Buffer.allocUnsafe(5); buf.copy(b, 0, i); console.log(b.toString()); }
const StringDecoder = require('string_decoder').StringDecoder; const decoder = new StringDecoder('utf8'); const buf = Buffer.from('中文字符串!'); for(let i = 0; i < buf.length; i+=5){ var b = Buffer.allocUnsafe(5); buf.copy(b, 0, i); console.log(decoder.write(b)); }
For more programming-related knowledge, please visit:Programming Video! !
The above is the detailed content of A brief analysis of Buffer in NodeJS. For more information, please follow other related articles on the PHP Chinese website!