Q23: What are Zero-Copy transfers and how are they achieved with Buffer.allocUnsafe()?

Buffer.alloc() initializes memory by zero-filling the allocated space, preventing accidental exposure of sensitive lingering data. Buffer.allocUnsafe() allocates memory without zero-filling, making execution faster but introducing potential data leak security risks if uninitialized memory is read directly. Zero-copy operations avoid unnecessary memory copying by pointing multiple abstractions or operations to the exact same underlying memory reference using typed arrays (ArrayBuffer) or network stream descriptors.

Key Takeaways
  • Buffer.allocUnsafe() skips memory zero-filling for higher performance, but requires immediate overwriting to avoid data leaks.
  • Zero-copy mechanisms share existing memory references to eliminate data duplication overhead during I/O.
Knowledge Check

What security risk arises when using Buffer.allocUnsafe() without writing new data over it immediately?