What are Buffers?
When working with data transmitted over a network or when reading and writing files on disk, we often encounter data that is not immediately available in its entirety. Buffers are temporary stores for data while it is being moved from one place to another. Buffers allow this data to be accumulated and processed in chunks, thus efficiently managing memory and processing speed. Buffers are practically sequences of bytes (read more about binary numbers in the article “Binary System”).
Buffers are used in different situations:
-
Data Flow Management:
Buffers are used when the server receives data faster than it can process it, and then buffers are used to temporarily store that data until the server can process it.
-
Performance Improvement:
Using a buffer, the program can continue running without waiting for each piece of data to be completely processed. It can significantly improve application performance, especially in network operations where latency and transfer speed are key factors.
-
Asynchronous Programming:
Buffers are crucial in asynchronous programming, allowing applications to perform input/output (I/O) operations without blocking. This means that the server can efficiently handle multiple requests simultaneously, making it capable of serving multiple users without slowing down.

Buffers in Node.js
Buffer is a class in Node.js designed to work with binary data. It allows direct reading from and writing to memory, which is much faster and more efficient for binary data than using strings. In modern versions of Node.js, it is not necessary to use require to call a Buffer object. Buffer is a global object in Node.js, which means that it is automatically available in every file without the need for explicit import. This makes working with binary data easier because you can use Buffer methods directly.
Example
|
1 2 3 |
// Direct use of Buffer methods without require const buffer = Buffer.from('This is an example of the text in the buffer.'); console.log(buffer.toString()); |
In Node.js, buffers are used to work with binary data. This means that each element in the buffer represents one byte of data. Buffers are crucial when working with any binary data, such as multimedia files (images, video, audio), because they allow efficient management of that data.
Creating a buffer
Buffer can be created in several ways:
- Buffer.from(): Creates a buffer from existing data, such as a string.
- Buffer.alloc(): Creates an empty buffer of a specified size, with initialized data.
- Buffer.allocUnsafe(): Similarly, it creates a buffer of a certain size, but without data initialization.
Example of buffer creation
|
1 2 3 4 5 6 |
// Creating a buffer from a string const buf = Buffer.from('hello'); // Creating a buffer from an array of numbers const buf = Buffer.from([1, 2, 3, 4, 5]); // Creating an empty buffer of 10 bytes const bufEmpty = Buffer.alloc(10); |
The first method creates a buffer of a certain size, the second creates a buffer from a string, and the third creates a buffer from a series of numbers.
After creation, the buffer can be used to store or manipulate binary data.
Reading and writing buffers
You can read and write to buffers using methods like buf.write() and buf.toString().
Example
For example, to write a string to a buffer and then read it:
|
1 2 3 |
// Writing "Hello" to an empty buffer bufEmpty.write("Hello", 0, 5, 'utf-8'); console.log(buf.toString()); |
This will print “hello” to the console, translated frombinary format that was saved in the buffer.
Usage example for conversions
In Node.js, we often use Buffers to convert binary data to hexadecimal strings or to work with different character encodings.
|
1 2 |
const buf = Buffer.from('Zdravo, svete!', 'utf8'); console.log(buf.toString('hex')); // Converting the contents of a Buffer to a hexadecimal string |
Maximum buffer size in Node.js
The buffer size depends on the version of Node.js and the architecture of the system on which it is executed (32-bit or 64-bit). In newer versions of Node.js, on 64-bit systems, the maximum buffer size can be up to about 2GB (gigabytes). This limitation is set due to a limitation in the V8 JavaScript engine that Node.js uses. To be more specific, it should be noted that the maximum size may differ depending on the version of the V8 engine and specific changes that the Node.js team may implement. Therefore, if you are working with very large buffers and are close to the limit of what V8 can handle, it is recommended to check the latest Node.js documentation or test in your specific environment.
Example of buffer usage
In this example, we’ll use Buffer to read and write binary data from a file by loading an image from a file, converting it to a base64 string, and writing it back to a new file:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import * as fs from 'fs'; import * as path from 'path'; // The path to the input image const inputImagePath = path.join(__dirname, 'input.jpg'); // The path to the output image const outputImagePath = path.join(__dirname, 'output.jpg'); // Reading the image into the buffer fs.readFile(inputImagePath, (err, data) => { if (err) { console.error('Error reading file:', err); return; } // Convert buffer to a base64 string const base64Image = data.toString('base64'); // Logovanje base64 string-a (opciono) console.log('Base64 Image:', base64Image); // Convert base64 string back to a buffer const imageBuffer = Buffer.from(base64Image, 'base64'); // Writing the buffer to a new file fs.writeFile(outputImagePath, imageBuffer, (err) => { if (err) { console.error('Error writing file:', err); return; } console.log('Image written successfully to', outputImagePath); }); }); |
