Table of Contents
Node.js Buffer method
结论
Home Web Front-end JS Tutorial Let's talk about some important methods of the Node.js buffer module

Let's talk about some important methods of the Node.js buffer module

Jan 05, 2022 pm 07:03 PM
buffer node.js buffer

This article will share with you a complete guide to the Node.js buffer and talk about some important methods of the Node buffer (Buffer) module. I hope it will be helpful to everyone!

Let's talk about some important methods of the Node.js buffer module

A binary stream is a large collection of binary data. Since the size of the binary stream is usually quite large, the binary stream is generally not shipped together, but is divided into small chunks before shipping and sent one by one.

When the data processing unit temporarily stops receiving other data streams, the remaining data will be retained in the cache until the data processing unit is ready to receive more data.

Node.js Servers generally need to read and write in the file system, and files are actually binary streams at the storage level. In addition, Node.js can also be used with TCP streams, allowing TCP streams to provide reliable end-to-end byte streams to ensure communication over unreliable Internet networks.

The data stream sent to the receiver is buffered until the receiver is ready to receive more data to process. This is what the temporary data part of Node.js does - managing and storing binary data outside of the V8 engine.

Let’s dive into the various ways to use buffers (Buffer), learn more about them and learn how to use them in Node.js programs.

Lets talk about some important methods of the Node.js buffer module

Node.js Buffer method

The biggest advantage of the Node.js buffer module is that it is built into Node.js, so we We can use it anywhere we want to use it.

Let’s go over some important Node.js buffer module methods.

Buffer.alloc()

This method will create a new buffer, but the allocated size is not fixed. When we call this method, we can allocate the size (in bytes) ourselves.

const buf = Buffer.alloc(6)  // 这会创建一个 6 字节的缓冲区

console.log(buf) // <Buffer 00 00 00 00 00 00>
Copy after login

Buffer.byteLength()

If we want to get the length of the buffer, we just call Buffer.byteLength () will do.

var buf = Buffer.alloc(10)
var buffLen = Buffer.byteLength(buf) // 检查缓冲区长度

console.log(buffLen) // 10
Copy after login

Buffer.compare()

By using Buffer.compare() we can compare two buffers , the return value of this method is one of -1, 0, 1.

Translator's Note: buf.compare(otherBuffer); This call will return a number -1, 0, 1, corresponding to buf before, after or the same as otherBuffer.

var buf1 = Buffer.from(&#39;Harsh&#39;)
var buf2 = Buffer.from(&#39;Harsg&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 0

var buf1 = Buffer.from(&#39;a&#39;)
var buf2 = Buffer.from(&#39;b&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 -1


var buf1 = Buffer.from(&#39;b&#39;)
var buf2 = Buffer.from(&#39;a&#39;)
var a = Buffer.compare(buf1, buf2)
console.log(a) // 这会打印 1
Copy after login

Buffer.concat()

As the name suggests, we can use this function to concatenate two buffers. Of course, just like strings, we can also concatenate more than two buffers.

var buffer1 = Buffer.from(&#39;x&#39;)
var buffer2 = Buffer.from(&#39;y&#39;)
var buffer3 = Buffer.from(&#39;z&#39;)
var arr = [buffer1, buffer2, buffer3]

console.log(arr)
/* buffer, !concat [ <Buffer 78>, <Buffer 79>, <Buffer 7a> ] */

// 通过 Buffer.concat 方法连接两个缓冲区
var buf = Buffer.concat(arr)

console.log(buf)
// <Buffer 78 79 7a> concat successful
Copy after login

Buffer.entries()

##Buffer.entries() will be created using the contents of this buffer And returns an iterator in the form [index, byte].

var buf = Buffer.from(&#39;xyz&#39;)

for (a of buf.entries()) {
    console.log(a)
    /* 这个会在控制台输出一个有缓冲区位置与内容的字节的数组 [ 0, 120 ][ 1, 121 ][ 2, 122 ] */
}
Copy after login

Buffer.fill()

We can use

Buffer.fill() This function inserts data into or Fill the buffer. See below for more information.

const b = Buffer.alloc(10).fill(&#39;a&#39;)

console.log(b.toString())
// aaaaaaaaaa
Copy after login

Buffer.includes()

Like a string, it will confirm whether the buffer has the value. We can achieve this using the

Buffer.includes() method, the given method returns a boolean value based on the search, i.e. true or false.

const buf = Buffer.from(&#39;this is a buffer&#39;)
console.log(buf.includes(&#39;this&#39;))
// true

console.log(buf.includes(Buffer.from(&#39;a buffer example&#39;)))
// false
Copy after login

Buffer.isEncoding()

We may know that binary files must be encoded, so if we want to check whether the data type supports character encoding what can we do about it? We can use the

Buffer.isEncoding() method to confirm. If supported, it will return true.

console.log(Buffer.isEncoding(&#39;hex&#39;))
// true

console.log(Buffer.isEncoding(&#39;utf-8&#39;))
// true

console.log(Buffer.isEncoding(&#39;utf/8&#39;))
// false

console.log(Buffer.isEncoding(&#39;hey&#39;))
// false
Copy after login

Buffer.slice()

##buf.slice()

will be used to use the selected buffer Element creates a new buffer - When a buffer is sliced, a new buffer is created containing a list of items to be found in the new buffer slice. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>var a = Buffer.from(&amp;#39;uvwxyz&amp;#39;); var b = a.slice(2, 5); console.log(b.toString()); // wxy</pre><div class="contentsignin">Copy after login</div></div>

Buffer.swapX()

Buffer.swapX()

The byte order used to swap buffers . Use Buffer.swapX() (where X can be 16, 32, 64) to swap the byte order of 16-bit, 32-bit and 64-bit buffer objects. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]) console.log(buf1) // &lt;Buffer 01 02 03 04 05 06 07 08&gt; // 交换 16 位字节顺序 buf1.swap16() console.log(buf1) // &lt;Buffer 02 01 04 03 06 05 08 07&gt; // 交换 32 位字节顺序 buf1.swap32() console.log(buf1) // &lt;Buffer 03 04 01 02 07 08 05 06&gt; // 交换 64 位字节顺序 buf1.swap64() console.log(buf1) // &lt;Buffer 06 05 08 07 02 01 04 03&gt;</pre><div class="contentsignin">Copy after login</div></div>

Buffer.json()It can help us create a JSON object from the buffer, and this method will return the JSON buffer object ,

const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

console.log(buf.toJSON());
// {"type":"Buffer", data:[1, 2, 3, 4, 5, 6, 7, 8]}
Copy after login

结论

如果我们需要进一步了解并使用 Node.js 的缓冲区,我们需要对缓冲区以及 Node.js 缓冲区的工作原理有更扎实的基础知识。我们还应该了解为什么我们需要使用 Node.js 缓冲区和各种 Node.js 缓冲区方法的使用。

更多node相关知识,请访问:nodejs 教程!!

The above is the detailed content of Let's talk about some important methods of the Node.js buffer module. 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)

Logger buffer size what is log used for Logger buffer size what is log used for Mar 13, 2023 pm 04:27 PM

The function is to provide engineers with feedback on usage information and records to facilitate problem analysis (used during development); because users themselves do not often generate upload logs, they are useless to users. The logging buffer is a small, temporary area used for short-term storage of change vectors for redo logs to be written to disk. A log buffer write to disk is a batch of change vectors from multiple transactions. Even so, the change vector in the log buffer is written to disk in near real-time, and when the session issues a COMMIT statement, the log buffer write operation is performed in real time.

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!

See all articles