InterviewHubPRO
Section Overview
Q28

Question 28 of 60

Part 3: Streams, Buffers & Core Modules (21 - 35)

47%
Node.js & Libuv•Part 3: Streams, Buffers & Core Modules (21 - 35)

`fs.readFile()` vs `fs.createReadStream()`.

In Simple Words:

  • fs.readFile(): Loads the entire file content into a memory Buffer before calling the callback. Fails with out-of-memory errors on large files.
  • fs.createReadStream(): Streams the file in 64KB chunks, allowing concurrent processing with minimal RAM footprint.
javascript
// Stream response directly to HTTP client:
app.get('/download', (req, res) => {
  const stream = fs.createReadStream('large_archive.zip');
  stream.pipe(res); // Streams chunks directly; never buffers full file
});