For utilising data we need it in some specific amount, if the receiving rate is faster or slower than the data processing speed, we need to hold data for a while, here comes Buffer.
In simple words, the Buffer can be considered a temporary storage to hold the data when the data receiving speed is faster or slower than the processing speed.
In Node.js, we can create a Buffer in three ways, using Buffer.from(), Buffer.alloc() and Buffer.allocUnsafe() methods. In this article, we will learn about Buffer.from() specifically, so let’s get started.
Node.js Buffer.from() Method
Node.js has a Buffer class to deal with binary data. The method Buffer.from() comes from this class and can be used to create a new Buffer instance by which we can convert strings, arrays, buffers, or array buffers into buffer objects having binary data. We can then also perform operations like reading, writing, encoding, decoding, etc.
Syntax
Below is the syntax for using the Buffer.from() Method.
Buffer.from(object, encoding);
Parameters:
This method takes up to two parameters, first is an object which can be any string, buffer, array, or array buffer, and second is its encoding which is an optional parameter, by default it is utf8. Encoding is used if the object is a string.
Return:
This method returns a Buffer object having binary data from the data passed as an argument.
Examples of Buffer.from() Method
Let’s see some examples to demonstrate the Buffer.from() method.
Example 1
Let’s say we want to create a Buffer from the string “Hello World”, then we can directly pass it to the Buffer.from() method without any argument, and it returns the Buffer object, which we can then print to see the data it contains using console.log() method.
// Input string to be converted to Buffer object
const inputString = "Hello World";
// Creating Buffer from string
const bufferFromStr = Buffer.from(inputString);
// No character encoding is specified, so the default value is 'utf-8'
// Print the Buffer return after conversion
console.log(bufferFromStr);
Output:
<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>4
The integer values in the Buffer object are basically hexadecimal code where each pair of hexadecimal digits corresponds to a single byte
Example 2
In example 1, we have passed a string as an input, let’s extend the example and created another Buffer by passing an array as an argument to the Buffer.from() method then validate the data type of both returned values using JavaScript built-in method typeof().
// Input string to be converted to Buffer object
const inputString = "Hello World";
// Creating Buffer from the string
const bufferFromStr = Buffer.from(inputString);
// No character encoding is specified, so the default value is 'utf-8'
// Print the Buffer return after conversion
console.log(bufferFromStr);
// Input array to be converted to Buffer object
const inputArray = [10, 12, 14, 16];
// Creating Buffer from the array
const bufferFromArray = Buffer.from(inputArray);
// Print the Buffer return after conversion
console.log(bufferFromArray);
// Checking the types of both Buffers
console.log("Type of bufferFromStr")
console.log(typeof(bufferFromStr))
console.log("Type of bufferFromArray")
console.log(typeof(bufferFromArray))
Output:
<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
<Buffer 0a 0c 0e 10>
Type of bufferFromStr
object
Type of bufferFromArray
object
In the above Node.js code, we can see that both the value return is of object data type, giving the ability to further perform operations on it.
Conclusion
In this article, we have seen the Buffer.from() method, its syntax, parameters, and return value along with two examples. At first look, Buffer can be seen as a boring concept, but it has some super cool use cases such as reading images, performing encryption, hashing, advanced file operations and much more. If you are a Node developer, you should definitely give it a try. If you want to read a more in-depth tutorial on Node.js Buffers click here. At last, we hope you have enjoyed reading the content.
Read More: Multiple Ways to Copy Files and Folders in NodeJS
Reference
https://nodejs.org/api/buffer.html