How to convert buffer to array in nodejs

PHPz
Release: 2023-04-17 16:50:30
Original
1798 people have browsed it

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:

  1. Use a for loop to traverse the buffer, convert each byte into a decimal number, and put it into the array.
function buffer2Array1(buf) {
  const arr = []
  for (let i = 0; i < buf.length; i++) {
    arr.push(buf[i])
  }
  return arr
}
Copy after login
  1. Use the spread operator to convert buffer to an array.
function buffer2Array2(buf) {
  return [...buf]
}
Copy after login
  1. Use the map function to convert the buffer into an array.
function buffer2Array3(buf) {
  return Array.prototype.map.call(buf, (x) => x)
}
Copy after login
  1. Use the from function to convert buffer into an array of type Uint8Array.
function buffer2Array4(buf) {
  return Array.from(new Uint8Array(buf))
}
Copy after login

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

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!