Home > Article > Web Front-end > How to convert buffer to array in nodejs
Buffer objects are often used in Node.js for data input and output, but in some scenarios buffers need to be converted into arrays. This article will introduce how to convert buffer to array in Node.js.
About the buffer object
The buffer object is an object in Node.js that specializes in processing binary data. It is similar to an array, but it stores bytes instead of 32-bit integers. The underlying implementation of the buffer object uses C to improve efficiency and enable fast reading and writing of data streams.
In Node.js, we usually use buffer objects for network communication, file operations, etc. The buffer object has a wide range of usage scenarios, and it can be considered one of the cores of Node.js.
About arrays
In conventional programming, arrays are one of the most commonly used data types. Different types of data can be stored and can be operated in many different ways, such as adding, deleting, modifying, and checking.
In JavaScript, arrays are also one of the most commonly used data types. JS arrays are particularly flexible. They can store different types of data, and many functional programming methods can be used to operate on arrays.
Buffer to array
In Node.js, we often need to convert buffer to array for operation. Here are some methods to convert buffer to array:
function buffer2Array1(buf) { const arr = [] for (let i = 0; i < buf.length; i++) { arr.push(buf[i]) } return arr }
function buffer2Array2(buf) { return [...buf] }
function buffer2Array3(buf) { return Array.prototype.map.call(buf, (x) => x) }
function buffer2Array4(buf) { return Array.from(new Uint8Array(buf)) }
Among the above methods, the first method is the simplest, but it is slightly insufficient in terms of performance. The second method uses the concise spread operator, which improves performance compared to the first method. The third method uses the ES5 Array.prototype.map function, which may be relatively more readable, but will also bring some performance losses. The last method is to use ArrayBuffer related methods for conversion, which performs relatively better in terms of performance.
Example Demonstration
In a simple example, you can directly call the above method to convert buffer to array. The following is an example of practical application:
const fs = require('fs') const fileName = 'test.txt' fs.readFile(fileName, (err, data) => { if (err) { console.error(err) return } const arr1 = buffer2Array1(data) const arr2 = buffer2Array2(data) const arr3 = buffer2Array3(data) const arr4 = buffer2Array4(data) console.log(arr1) console.log(arr2) console.log(arr3) console.log(arr4) })
In this code, we use the fs module of Node.js to read a file, then convert it into an array, and output the results of the four methods .
Summary
The buffer object is an object in Node.js that specializes in processing binary data, and arrays are one of the commonly used data types. In Node.js, we often need to convert buffers into arrays for operations. This article introduces four methods for converting buffers into arrays, and demonstrates an actual application scenario with an example.
The above is the detailed content of How to convert buffer to array in nodejs. For more information, please follow other related articles on the PHP Chinese website!