What is Buffer.from() in Node.js?

The Buffer.from() function creates a new buffer and can be used in multiple ways.

Prototype 1

Explanation

The above prototype of the Buffer.from() function accepts an integer array that contains bytes in the range 0-255 and uses the bytes to create a buffer.

If the byte elements of the array lie outside the range 0-255, they are truncated to fit into the range.

Example

The following example demonstrates how to use an array to create a buffer with the Buffer.form(array) function.

//create an array
const array = [0x12, 0x23, 0x13, 0x45, 0x31]
//creat the buffer using the array
const buf = Buffer.from(array)
console.log(buf)

Prototype 2

Explanation

The above prototype of the Buffer.from() function accepts the following parameters:

  • arrayBuffer: An ArrayBuffer or a SharedArrayBuffer with a .Buffer property.
  • byteOffset: The byte from where the function starts to create the buffer. byteOffset is set to 0 by default.
  • length: The number of bytes to include in the buffer. length is set to arrayBuffer.byteLength - byteOffset by default.

Note: The underlying array and the buffer share the same memory.

Example

The following example demonstrates how to use ArrayBuffer to create a buffer though the Buffer.from(arrayBuffer[, byteOffset[, length]]) function.

The program below shares memory with the underlying array. Therefore, when the array changes, the buffer changes as well. buf2 employs the byteoffset and length parameters to ensure that only the third element of the array is created in a buffer.

// create an ArrayBuffer
const array = new Uint8Array(4);
array[0] = 23;
array[1] = 97;
array[2] = 33;
array[3] = 40;
//create a buffer having a shared memory with the array
const buf = Buffer.from(array.buffer);
console.log(buf);
// change the array
array[1] = 145;
// the buffer changes as well
console.log(buf);
const buf2 = Buffer.from(array.buffer, 2,1);
console.log(buf2);

Prototype 3

Explanation

This prototype of the Buffer.from() function accepts buffer of type Buffer or Uint8Array as a single input. Buffer.from() copies the data from the input to the created buffer.

Example

The following example demonstrates how to create a buffer using an existing buffer with the Buffer.from( buffer) function.

//create an arra
const array = [22, 33, 44, 55, 66]
//create a buffer
const buf1 = Buffer.from(array)
console.log(buf1)
//create another buffer using an existing buffer
const buf2 = Buffer. from(buf1)
console.log(buf2)

Prototype 4

Explanation

This prototype of the Buffer.from() function accepts the following parameters:

  • object: An object that supports the Symbol.toPrimitive() or ValueOf() method.
  • offsetOrEncoding: The byte offset of type integer, or an encoding of type string.
  • length: The number of bytes to include in the buffer.

Example

The following example demonstrates how to create a buffer from objects that contain the Symbols.toPrimitive() or valueOf() method.

// create an object with Symbol.toPrimitive method
class example {
[Symbol.toPrimitive]() {
return 'The example class has a Symbol.toPrimitive() method';
}
}
const ex = new example()
// create a buffer using the object
const buf = Buffer.from( ex, 'utf8');
console.log(buf)
const str = "string has valuOf method"
console.log(str.valueOf())
// create a buffer using the object with valueOf method
const buf1 = Buffer.from( ex, 'utf8');
console.log(buf1)

Prototype 5

Explanation

This prototype of the Buffer.from() function accepts the following parameters:

  • string: The string that is encoded and made into a buffer.
  • encoding: The encoding type, set to utf8 by default.

Example

The following code demonstrates how to create a buffer from strings with the Buffer.from(string [, encoding]) function.

//encode with utf8
const buf1 = Buffer.from('a buffer created from a string');
//encode with hex
const buf2 = Buffer.from('2074c3a97374468697', 'hex');
console.log(buf1)
console.log(buf2)
Copyright ©2024 Educative, Inc. All rights reserved