It is a built-in object in Node.js that represents binary data.
Buffers are commonly used for reading and writing binary data, from and to files, working with network protocols, and cryptography, and handling data in binary formats like images, audio, and video.
buffers were introduced in Node.js to provide a proper set of APIs to manipulate bits and bytes in an easy and performant way.
Handling Binary Data: Buffers are used when dealing with TCP streams or performing read-write operations on the file system, which require handling pure binary data.
Efficiency: Buffers provide a fast and efficient way to store and manipulate binary data in Node.js. They are particularly useful when you're interacting with binary data at lower networking levels.
Memory Management: Buffers refer to a particular memory location. Unlike arrays, which can be of any type and resizable, buffers only deal with binary data and are not resizable. This makes them more memory efficient.
Data Manipulation: Buffers equip you with the ability to do fine-grained data manipulation in Node.js. For example, you might need to concatenate two binary data streams, slice a large binary file into smaller pieces, or encode and decode binary data into different character encodings.
You need to read a small image file and send it over an HTTP response. How would you use a Buffer to do this without loading the whole file into a string?
What happens if you concatenate two Buffer objects with the '+' operator instead of Buffer.concat?
Our file‑upload endpoint streams data to disk, but occasionally the saved file is corrupted. Walk me through how you would check the Buffer handling in the stream pipeline.
During a refactor we switched from callbacks to async/await for a TCP socket server, and now we see occasional back‑pressure stalls. How would you use Buffers to diagnose and resolve the issue?
Design a high‑throughput logging service that writes millions of log entries per second to disk. How would you batch data with Buffers, and what trade‑offs do you consider regarding memory usage and flush frequency?
Your service processes large protobuf messages and you notice increased GC pause times. How would you restructure Buffer allocation and reuse to improve throughput and reduce latency?
We plan to move zero‑copy data transfer between Node.js services and a C++ backend using shared memory. How would you architect Buffer usage across the boundary, and what long‑term maintenance concerns would you address?
When migrating a monolith to microservices, you need a company‑wide binary protocol. What guidelines would you set for Buffer allocation, lifecycle, and versioning to avoid memory leaks and ensure cross‑team compatibility?