← Back to Node.js, page 1

nodejs-0013 · Medium mid

What is the difference between Buffer.alloc, Buffer.allocUnsafe and Buffer.from, and does subarray copy data?

Buffer.alloc returns zero-filled memory. Buffer.allocUnsafe skips the zero-fill and may contain old data, so it must be fully overwritten before use. Buffer.from copies its input. subarray does not copy: it returns a view over the same memory.

Full answer

Three ways to create a Buffer:

  • Buffer.alloc(n): n bytes, all set to zero. Safe and a bit slower.
  • Buffer.allocUnsafe(n): n bytes that were not cleared. They can hold leftovers from earlier use of the memory, such as parts of another request. If you send such a buffer to a client without overwriting every byte, you can leak secrets. Use it only when you will fill the whole buffer immediately, for speed.
  • Buffer.from(x): creates a Buffer from a string, an array, or another Buffer. From another Buffer or array it copies the data.

Memory pool: small buffers made by allocUnsafe and by Buffer.from(string) (smaller than half of Buffer.poolSize) are cut from a shared pool. The pool was 8 KB for many years and is 64 KB on Node 24. That avoids many tiny allocations, but it means buf.buffer (the underlying ArrayBuffer) is larger than the buffer itself. Never read buf.buffer directly, and if a tiny buffer must live long, use Buffer.allocUnsafeSlow or copy it, so it does not keep the whole pool alive.

buf.subarray(start, end) (and the older buf.slice) returns a new Buffer object that points to the same memory. Changing one changes the other. To get an independent copy, use Buffer.from(buf) or Buffer.copyBytesFrom.

javascript
const zeroed = Buffer.alloc(4);
console.log('alloc is all zeros:', zeroed.every((byte) => byte === 0));
console.log('alloc underlying ArrayBuffer size:', zeroed.buffer.byteLength);

const fast = Buffer.allocUnsafe(4);
console.log('allocUnsafe length:', fast.length, '| underlying ArrayBuffer size:', fast.buffer.byteLength);
console.log('Buffer.poolSize:', Buffer.poolSize);

// subarray shares memory, Buffer.from copies it.
const original = Buffer.from('abcdef');
const view = original.subarray(0, 3);
view[0] = 0x5a; // 'Z'
console.log('after changing the view:  ', original.toString());

const copy = Buffer.from(original);
copy[0] = 0x41; // 'A'
console.log('after changing the copy:  ', original.toString(), '| copy:', copy.toString());

The underlying ArrayBuffer sizes show that a small unsafe buffer lives inside the shared pool (Buffer.poolSize bytes), while alloc gets its own 4 bytes. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): alloc is all zeros: true alloc underlying ArrayBuffer size: 4 allocUnsafe length: 4 | underlying ArrayBuffer size: 65536 Buffer.poolSize: 65536 after changing the view: Zbcdef after changing the copy: Zbcdef | copy: Abcdef

Key points

  • alloc zero-fills. allocUnsafe does not, and can expose old data.
  • Small buffers from allocUnsafe and Buffer.from(string) come from a shared pool (Buffer.poolSize).
  • subarray shares memory. Buffer.from(buffer) copies.
  • Overwrite every byte of an allocUnsafe buffer before use.

Common mistakes

  • Using allocUnsafe for performance and sending the result without filling it.
  • Assuming slice or subarray copies the data.
  • Reading buf.buffer and expecting only the bytes of that Buffer.
  • Keeping a tiny pooled buffer for a long time and holding the whole pool in memory.

Follow-ups

  • Why did Node add the pool?
  • What is the difference between Buffer.slice and Uint8Array.prototype.slice?
  • How would you securely clear a buffer holding a secret?

Tips

  • Name the security risk of allocUnsafe in one sentence.
  • Say subarray is a view, and give the example that proves it.
  • Mention zero-fill cost as the only reason to use allocUnsafe.