
Node.js Buffers Explained: Handling Binary Data Efficiently
In Node.js, a buffer class object is used to represent a fixed-length byte sequence. It refers to a raw memory allocation outside of the V8 heap and stores raw data similarly to an array of integers. JavaScript’s Uint8Array class is a subclass of Node.js’s Buffer class. Despite being a global object, an instance Buffer class should ideally be explicitly accessed using an import or need declaration.
A buffer object is declared using the new operator − in previous iterations of Node.js.
var buf =newBuffer(10);
A buffer object can also be created from a given array −
var buf =newBuffer([10,20,30,40,50]);
or from a given string −
var buf =newBuffer("Simply Easy Learning","utf-8");
However, the use of new keyword has now been deprecated. You need to use the following static methods to create a Buffer object −
// Creates a zero-filled Buffer of length 10.const buf1 = Buffer.alloc(10);// Creates an uninitialized buffer of length 10.const buf2 = Buffer.allocUnsafe(10);// Creates a Buffer containing arrayconst buf3 = Buffer.from([1,2,3]);// creates a buffer from stringconst buf4 = Buffer.from('hello world','utf8');
Static methods
alloc()
Allocates a new Buffer of specified size in bytes.
Buffer.alloc(size[, fill[, encoding]])
Specifications
- size: The new buffer’s intended length.
- fill − A value that will be used to pre-fill the new buffer. By default: 0.
- encoding: This is the encoding of a string, if fill is one. ‘utf8’ is the default.
Example
Open Compiler
const buf = Buffer.alloc(5); console.log(buf);
Output
<Buffer 00 00 00 00 00>
allocUnsafe()
Creates an uninitialized buffer of specified size in bytes
Buffer.allocUnsafe(size)
Example
Open Compiler
const buf = Buffer.allocUnsafe(10); console.log(buf); buf.fill('a'); console.log(buf);
Output
<Buffer 38 67 ff aa 32 56 00 00 08 00> <Buffer 61 61 61 61 61 61 61 61 61 61>
from()
Allocates a new Buffer using an array of bytes in the range 0 – 255.
Example
Open Compiler
// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.const buf1 = Buffer.from([0x62,0x75,0x66,0x66,0x65,0x72]); console.log(buf1);// Creates a new Buffer from integer array.const buf2 = Buffer.from([10,20,30,40,50]); console.log(buf2);
Output
<Buffer 62 75 66 66 65 72> <Buffer 0a 14 1e 28 32>
from()
Creates a new Buffer containing string.
Buffer.from(string[, encoding])
Parameters
string <string> A string to encode.
encoding <string> The string is encoded. ‘utf8’ is the default. The character encoding to be applied when converting a string to bytes is specified by the encoding option.
Example
Open Compiler
const buf1 = Buffer.from('Hello World'); console.log(buf1); console.log(buf1.toString());
Output
<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64> Hello World
compare()
Compares a buffer object with other buffer object.
compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])
Parameters
- target − A buffer that can be used for comparison.
- targetStart − The offset inside the target where the comparison should start. By default: 0.
- targetEnd − The offset (not inclusive) within the target at which the comparison should finish. set to target.length by default.
- sourceStart − The offset in the source buffer where the comparison should start. By default: 0.
- sourceEnd − The offset (not inclusive) in the source buffer where the comparison should terminate. buf.length by default.
The function returns a number indicating whether the first buffer comes before, after, or is the same as the other buffer object in sort order.
0 is returned if second is the same as first
1 is returned if second buffer should come before the first when sorted.
-1 is returned if second buffer should come after the first when sorted.
Example
Open Compiler
var buffer1 = Buffer.from('BALL');var buffer2 = Buffer.from('BAT');var result = buffer1.compare(buffer2);if(result <0){ console.log(buffer1 +" comes before "+ buffer2);}elseif(result ===0){ console.log(buffer1 +" is same as "+ buffer2);}else{ console.log(buffer1 +" comes after "+ buffer2);}
Output
BALL comes before BAT
In the above example, change the two Buffer objects to −
var buffer1 = Buffer.from('CURE');var buffer2 = Buffer.from('CORE');
Output
CURE comes after CORE
copy()
Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source.
copy(target[, targetStart[, sourceStart[, sourceEnd]]])
Parameters
- target − A Buffer to copy into.
- targetStart − The offset within target at which to begin writing. Default: 0.
- sourceStart − The offset within buf from which to begin copying. Default: 0.
- sourceEnd − The offset within buf at which to stop copying (not inclusive). Default: buf.length.
Example
Open Compiler
var buffer1 = Buffer.from('Hello World');var buffer2 = Buffer.allocUnsafe(buffer1.length);var result = buffer1.compare(buffer2); buffer1.copy(buffer2); console.log("buffer2 content: "+ buffer2.toString());
Output
buffer2 content: Hello World
entries()
returns an iterator of [index, byte] pairs from the contents of buffer object.
Example
Open Compiler
var buf = Buffer.from('Hello World');for(const pair of buf.entries()){ console.log(pair);}
Output
[ 0, 72 ] [ 1, 101 ] [ 2, 108 ] [ 3, 108 ] [ 4, 111 ] [ 5, 32 ] [ 6, 87 ] [ 7, 111 ] [ 8, 114 ] [ 9, 108 ] [ 10, 100 ]
slice()
returns a new Buffer cropped by given indices
buf.slice([start][, end])
Parameters
- start − Number, Optional, Default: 0
- end − Number, Optional, Default: buffer.length
Return Value
Returns a new buffer, offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.
Example
Open Compiler
var buffer1 = Buffer.from('thefullstack');//slicing a buffervar buffer2 = buffer1.slice(0,9);
console.log("buffer2 content: "+ buffer2.toString());
Output
buffer2 content: thefullstack
write()
Writes string to a buffer at offset according to the specified encoding.
write(string[, offset[, length]][, encoding])
Parameters
- string − String to write to buf.
- offset − Number of bytes to skip before starting to write string. Default: 0.
- length − Maximum number of bytes to write (written bytes will not exceed buf.length – offset). Default: buf.length – offset.
- encoding − The character encoding of string. Default: ‘utf8’.
The function returns the number of bytes written.
Example
Open Compiler
const buf = Buffer.alloc(256);const len = buf.write("Simple Easy Learning"); console.log(`${len} bytes: ${buf.toString('utf8', 0, len)}`);
Output
20 bytes: Simple Easy Learning
toJSON()
Returns a JSON representation of a given buffer object.
Example
Open Compiler
const buf = Buffer.from("Simple Easy Learning");var json = buf.toJSON(); console.log(json);
Output
{ type: 'Buffer', data: [ 83, 105, 109, 112, 108, 101, 32, 69, 97, 115, 121, 32, 76, 101, 97, 114, 110, 105, 110, 103 ] }
concat()
Returns a new Buffer which is the result of concatenating all the Buffer instances.
concat(list[, totalLength])
Parameters
- list − List of Buffer or Uint8Array instances to concatenate.
- totalLength − Total length of the Buffer instances in list when concatenated.
Example
Open Compiler
var buffer1 = Buffer.from('thefullstack ');var buffer2 = Buffer.from('Simply Easy Learning');var buffer3 = Buffer.concat([buffer1,buffer2]);
console.log("buffer3 content: "+ buffer3.toString());
Output
buffer3 content: thefullstack Simply Easy Learning
Methods Reference
Following is a reference of Buffers module available in Node.js. For more detail, you can refer to the official documentation.
Class Methods
Sr.No | Method & Description |
---|---|
1 | Buffer.isEncoding(encoding)Returns true if the encoding is a valid encoding argument, false otherwise. |
2 | Buffer.isBuffer(obj)Tests if obj is a Buffer. |
3 | Buffer.byteLength(string[, encoding])Gives the actual byte length of a string. encoding defaults to ‘utf8’. It is not the same as String.prototype.length, since String.prototype.length returns the number of characters in a string. |
4 | Buffer.concat(list[, totalLength])Returns a buffer which is the result of concatenating all the buffers in the list together. |
5 | Buffer.compare(buf1, buf2)The same as buf1.compare(buf2). Useful for sorting an array of buffers. |
It might ne helpful:
Best Practices for API Design in Full-Stack Development
Model-Based Security Testing: Ensuring Software Security with MBT
Leave a Reply