01 / 06

What are buffers?

It is a built-in object in Node.js that represents binary data.

  1. 1

    It is useful when working with binary streams or when interfacing with non-JavaScript systems or protocols.

  2. 2

    Buffer objects are used to represent a fixed-length sequence of bytes. Many Node.js APIs support Buffers.

  3. 3

    The Buffer class is a subclass of JavaScript's Uint8Array class, extending it with methods that cover additional use cases.

  4. 4

    While the Buffer class is available within the global scope, it is still recommended to reference it via an import or require statement explicitly.

  5. 5

    They are used to create multiple chunks of data for processing instead of all of it immediately.

  6. 6

    It is used in storing streaming data before processing.

Difficulty: 6/10
Topics: Memory Management, Binary Data & Streams, Performance Optimization

Scenario Questions

0-2 years experience
  1. 1

    Imagine you're writing a script to read an image file from disk and upload it to an external API that expects a base64-encoded string. How would you read that file in Node.js and convert it to the correct format without losing data?

  2. 2

    You are reading a text file using fs.readFile without specifying an encoding, and when you console.log the output, you see a bunch of hexadecimal numbers like <Buffer 48 65 6c 6c 6f>. Why is this happening, and how would you get the actual text out of it?

2-5 years experience
  1. 1

    We have an endpoint that processes incoming CSV uploads. A developer used Buffer.allocUnsafe() to quickly allocate space for parsing, but occasionally, sensitive data from previous HTTP requests is leaking into the parsed output. Why is this happening, and how would you fix it while keeping performance in mind?

  2. 2

    You're writing a TCP socket server that receives data in chunks. You need to accumulate these chunks until a delimiter is found. If you just keep doing totalBuffer = Buffer.concat([totalBuffer, chunk]) on every data event, what performance issues will you run into as the payload grows, and how would you optimize this?

5-8 years experience
  1. 1

    We are running a Node.js gateway service that proxies large video files. We're seeing container OOM (Out of Memory) crashes even though the V8 heap usage reported by APM looks completely fine. How would you investigate this discrepancy, and how does Node's buffer allocation strategy outside the V8 heap play into this?

  2. 2

    You are designing a high-throughput parser for a custom binary protocol over WebSockets. You need to parse headers, extract payloads, and minimize garbage collection overhead. How would you leverage SharedArrayBuffer, TypedArrays, or Node's Buffer.subarray to achieve zero-copy slicing?

8+ years experience
  1. 1

    Our enterprise platform processes millions of PDF generation requests daily. We are hitting bottlenecks where the event loop gets blocked during heavy buffer serialization/deserialization, and memory fragmentation is causing long-term process degradation. How would you architect a solution—perhaps involving worker threads, native C++ addons, or off-heap memory management—to isolate and scale this workload?

  2. 2

    We are migrating a legacy Node.js codebase from an older version to a modern LTS release. The codebase heavily relies on deprecated new Buffer() constructors and direct memory manipulation. How would you design a migration strategy and linting/runtime guardrails to safely transition the team to modern Buffer APIs without introducing security vulnerabilities or performance regressions?

Follow-up Questions

  • What is the difference between Buffer.alloc and Buffer.allocUnsafe, and when would you use each?
  • How does Node's internal Buffer pool work under the hood for small allocations?
  • How do Buffers relate to V8's garbage collector, and why might process memory look high while heap memory looks low?