WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Intermediate · question 24 of 100

What are the differences between ’fs.readFile’ and ’fs.createReadStream’ for reading a file in Node.js?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

In Node.js, both ‘fs.readFile‘ and ‘fs.createReadStream‘ are used for reading files, but they have different use cases and behaviors. Here’s an overview of the differences:

1. **Memory Usage**: ‘fs.readFile‘ loads the entire file into memory as a Buffer, whereas ‘fs.createReadStream‘ reads the file in chunks, allowing for better memory management.

‘fs.readFile‘:

const fs = require('fs');

fs.readFile('path/to/file', 'utf-8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

‘fs.createReadStream‘:

const fs = require('fs');
const readStream = fs.createReadStream('path/to/file', 'utf-8');

readStream.on('data', (chunk) => {
  console.log(chunk);
});

readStream.on('end', () => {
  console.log('File reading completed.');
});

2. **Asynchrony**: Both ‘fs.readFile‘ and ‘fs.createReadStream‘ are asynchronous; however, the way they handle data is different. ‘fs.readFile‘ reads the complete file and then returns the data as a single buffer or string when the reading is done. On the other hand, ‘fs.createReadStream‘ emits ’data’ events when it reads data, allowing you to process the data in real-time.

3. **Concurrency**: When working with large files or processing files concurrently, ‘fs.createReadStream‘ provides better performance because it reads data in chunks and doesn’t consume a lot of memory, while ‘fs.readFile‘ can lead to higher memory consumption and slower performance.

4. **Error Handling**: When an error occurs (e.g., file does not exist), ‘fs.readFile‘ will pass the error to the callback function. In contrast, ‘fs.createReadStream‘ will emit an ’error’ event to handle the error.

5. **Piping**: ‘fs.createReadStream‘ creates a readable stream, which means it can be piped to writable streams (e.g., HTTP response), allowing for efficient data transfer. While ‘fs.readFile‘ does not directly support piping, you can manually create a writable stream and write the data to it.

Example of piping with ‘fs.createReadStream‘ for serving a large file:

const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
  const readStream = fs.createReadStream('large-file.txt');
  readStream.pipe(res);
});

server.listen(3000, () => {
  console.log('Server is listening on port 3000');
});

In summary, ‘fs.readFile‘ is suitable when you need to read small files and process the data at once, while ‘fs.createReadStream‘ is recommended for larger files or when you need to process the data in chunks, such as streaming large files to clients or processing data concurrently.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics