Search This Blog

2023/09/28

Node.js:Buffer Interview Questions

 What is a Node.js Buffer?


A Node.js Buffer is a built-in object in Node.js used to
represent binary data directly in memory. It is a raw memory
allocation outside the JavaScript heap that allows Node.js to
handle binary data efficiently, which is crucial for tasks like
working with files, network streams, and other data sources
where binary data is involved.

How can you create a Buffer in Node.js?

You can create a Buffer in Node.js using one of the following methods:

Buffer.from(data): Creates a Buffer from an array, array-like object, or string.
Buffer.alloc(size): Allocates a new, zero-filled Buffer of the specified size.
Buffer.allocUnsafe(size): Allocates a new Buffer of the specified size, but it
may contain uninitialized memory.
Buffer.allocUnsafeSlow(size): Similar to Buffer.allocUnsafe(size), but slower.

How do you read data from a Buffer?

You can read data from a Buffer using methods like readUInt8,
readUInt16LE, readUInt16BE, readUInt32LE, readUInt32BE,
and similar methods for different data types. For example:

const buffer = Buffer.from([0x01, 0x02, 0x03, 0x04]);
const value = buffer.readUInt16LE(0); // Read a 16-bit integer in little-endian format from offset 0

How do you write data to a Buffer?

You can write data to a Buffer using methods like writeUInt8,
writeUInt16LE, writeUInt16BE, writeUInt32LE, writeUInt32BE,
and similar methods for different data types. For example:

const buffer = Buffer.alloc(4);
buffer.writeUInt16LE(0x0102, 0); // Write a 16-bit integer in little-endian format to offset 0


What are the differences between Buffer.from and Buffer.alloc?

Buffer.from creates a new Buffer and initializes it with the provided data (e.g., an array or string).
Buffer.alloc creates a new Buffer and initializes it with zeros.
Buffer.from is used when you want to create a Buffer from existing data,
whereas Buffer.alloc is used when you want to create an empty Buffer with a specific size.

How can you convert a Buffer to a string in Node.js?

Answer: You can convert a Buffer to a string in Node.js using the toString method. For example:

const buffer = Buffer.from('Hello, World!');
const str = buffer.toString('utf8'); // Convert the Buffer to a UTF-8 encoded string


What is the purpose of Buffer.allocUnsafe and Buffer.allocUnsafeSlow?

Answer:

Buffer.allocUnsafe allocates a new Buffer of the specified size but does not initialize its contents.
It is faster than Buffer.alloc because it doesn't fill the buffer with zeros,
but you should be careful when using it as the buffer may contain random data.

Buffer.allocUnsafeSlow is similar to Buffer.allocUnsafe, but it may be slower
and should be used only in cases where Buffer.allocUnsafe is not available.

How can you check the length of a Buffer?

Answer: You can check the length of a Buffer using the length property or the byteLength method:

const buffer = Buffer.from('Hello');
const length1 = buffer.length; // 5
const length2 = buffer.byteLength; // 5

No comments:

Post a Comment