Let's talk about some important methods of the Node.js buffer module
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!
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.
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>
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
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('Harsh') var buf2 = Buffer.from('Harsg') var a = Buffer.compare(buf1, buf2) console.log(a) // 这会打印 0 var buf1 = Buffer.from('a') var buf2 = Buffer.from('b') var a = Buffer.compare(buf1, buf2) console.log(a) // 这会打印 -1 var buf1 = Buffer.from('b') var buf2 = Buffer.from('a') var a = Buffer.compare(buf1, buf2) console.log(a) // 这会打印 1
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('x') var buffer2 = Buffer.from('y') var buffer3 = Buffer.from('z') 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
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('xyz') for (a of buf.entries()) { console.log(a) /* 这个会在控制台输出一个有缓冲区位置与内容的字节的数组 [ 0, 120 ][ 1, 121 ][ 2, 122 ] */ }
Buffer.fill()
Buffer.fill() This function inserts data into or Fill the buffer. See below for more information.
const b = Buffer.alloc(10).fill('a') console.log(b.toString()) // aaaaaaaaaa
Buffer.includes()
Buffer.includes() method, the given method returns a boolean value based on the search, i.e.
true or
false.
const buf = Buffer.from('this is a buffer') console.log(buf.includes('this')) // true console.log(buf.includes(Buffer.from('a buffer example'))) // false
Buffer.isEncoding()
Buffer.isEncoding() method to confirm. If supported, it will return
true.
console.log(Buffer.isEncoding('hex')) // true console.log(Buffer.isEncoding('utf-8')) // true console.log(Buffer.isEncoding('utf/8')) // false console.log(Buffer.isEncoding('hey')) // false
Buffer.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. <pre class='brush:php;toolbar:false;'>var a = Buffer.from(&#39;uvwxyz&#39;);
var b = a.slice(2, 5);
console.log(b.toString());
// wxy</pre>
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. <pre class='brush:php;toolbar:false;'>const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8])
console.log(buf1)
// <Buffer 01 02 03 04 05 06 07 08>
// 交换 16 位字节顺序
buf1.swap16()
console.log(buf1)
// <Buffer 02 01 04 03 06 05 08 07>
// 交换 32 位字节顺序
buf1.swap32()
console.log(buf1)
// <Buffer 03 04 01 02 07 08 05 06>
// 交换 64 位字节顺序
buf1.swap64()
console.log(buf1)
// <Buffer 06 05 08 07 02 01 04 03></pre>
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]}
结论
如果我们需要进一步了解并使用 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!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
