Home Web Front-end JS Tutorial What is a Buffer object in node.js? What is the usage scenario?

What is a Buffer object in node.js? What is the usage scenario?

Jul 28, 2021 pm 06:41 PM
node.js

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.

What is a Buffer object in node.js? What is the usage scenario?

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>
Copy after login

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

  • Can be used to process large amounts of binary data
  • Processing images, file reception and upload, network protocols Wait

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.

What is a Buffer object in node.js? What is the usage scenario?

What is a Buffer object in node.js? What is the usage scenario?

var http = require(&#39;http&#39;);
var str = "";

for (var i = 0; i < 1024 * 10; i++) {
    str += "a";
}

str = new Buffer.from(str,&#39;utf-8&#39;);

http.createServer(function (req, res) {
    res.writeHead(200);
    res.end(bufstr);
}).listen(8002);
Copy after login

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:

  • Writable - a stream that can write data (such as fs.createWriteStream())
  • Readable - A stream from which data can be read (e.g. fs.createReadStream()).
  • Duplex - a readable and writable stream (e.g. net.Socket).
  • Transform - 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Node.js 19 is officially released, let's talk about its 6 major features! Node.js 19 is officially released, let's talk about its 6 major features! Nov 16, 2022 pm 08:34 PM

Node 19 has been officially released. This article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to you!

Let's talk about the GC (garbage collection) mechanism in Node.js Let's talk about the GC (garbage collection) mechanism in Node.js Nov 29, 2022 pm 08:44 PM

How does Node.js do GC (garbage collection)? The following article will take you through it.

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

See all articles