This article will introduce you to the Buffer object in node.js, and see what the usage scenarios of the Buffer object are and what their advantages are.
The Buffer object is the core module of Node. It is also a test question that is frequently asked during interviews. If you don’t delve into it in depth, you may just know it like me. Yes, once the interviewer expands and asks more questions, it may not be possible. Anyway, at that time, I could only answer: The Buffer module is rarely used in the current business involved, but the Buffer object can effectively optimize some business functions and performance that cannot be satisfied by string transmission. [Recommended learning: "nodejs Tutorial"]
So, let’s learn about the Buffer object today.
Buffer object
The Buffer object is used to represent a fixed-length byte sequence
// 创建一个长度为 10、以零填充的 Buffer。 const buf1 = Buffer.alloc(10); // 创建一个长度为 10 的 Buffer, // 其中全部填充了值为 `1` 的字节。 const buf2 = Buffer.alloc(10, 1); var str="hello Jasen"; var buf = new Buffer.from(str,'utf-8'); console.log(buf); //输出:<Buffer 68 65 6c 6c 6f 20 4a 61 73 65 6e>
Observe the output result, Buffer Object is like an array.
Each element in the Buffer object is a two-digit hexadecimal number (that is, a value from 0 to 255)
If the value assigned to an element of the Buffer is less than 0, it will be added one after another. 256, until a value in the range of 0 to 255 is obtained. If it is greater than 255, subtract 256 one by one until a value in the range of 0-255 is obtained. If it is a decimal, it is rounded directly.
Usage scenarios
Advantages
Performance improvement during network transmission
Most of Strings are used during network transmission, which inevitably needs to be converted into a Buffer for data transmission in binary mode. If we directly convert it into a Buffer before transmitting, there is no need to do additional conversion during the transmission process, and loss is avoided, which improves performance.
The following is a performance test through ab, and the result of initiating 200 concurrent client requests. The QPS improvement is not very high, but there is still a slight improvement. You can copy the code and execute it to see if the result is the same as mine.
var http = require('http'); var str = ""; for (var i = 0; i < 1024 * 10; i++) { str += "a"; } str = new Buffer.from(str,'utf-8'); http.createServer(function (req, res) { res.writeHead(200); res.end(bufstr); }).listen(8002);
Buffer and Stream
Stream is also the core module of Node. The data is like Like water, a stream is an abstraction of input and output devices. It is a set of ordered byte data transmission methods with a starting point and an end point.
There are four basic stream types in Node.js:
Duplex
stream that can modify or convert data during the reading and writing process Scenario:
File upload and download in pieces, For example, when downloading a movie, you can watch it while downloading, such an implementation process.
Original address: https://juejin.cn/post/6955490895131066382
For more programming-related knowledge, please visit: Introduction to Programming! !
The above is the detailed content of What is a Buffer object in node.js? What is the usage scenario?. For more information, please follow other related articles on the PHP Chinese website!