Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a stream in Node.js?
A stream is a way to handle reading or writing data piece by piece, instead of all at once. It helps process large files efficiently without using much memory.
Click to reveal answer
beginner
Why might loading an entire file into memory be a problem?
Loading a whole file at once can use a lot of memory, especially if the file is large. This can slow down or crash your program if memory runs out.
Click to reveal answer
intermediate
Name two types of streams in Node.js.
Readable streams (for reading data) and writable streams (for writing data). There are also duplex streams that can do both.
Click to reveal answer
intermediate
How do streams improve performance when working with large files?
Streams process data in small chunks, so they use less memory and start working immediately without waiting for the whole file to load.
Click to reveal answer
beginner
What Node.js module is commonly used to work with file streams?
The built-in 'fs' module provides methods like createReadStream() and createWriteStream() to work with file streams.
Click to reveal answer
What happens when you load a large file entirely into memory in Node.js?
AIt splits the file into chunks automatically
BIt can use a lot of memory and slow down the program
CIt always runs faster than streams
DIt prevents any errors from happening
✗ Incorrect
Loading a large file fully uses much memory and can slow or crash the program.
Which method creates a readable stream for a file in Node.js?
Afs.createWriteStream()
Bfs.readFile()
Cfs.writeFile()
Dfs.createReadStream()
✗ Incorrect
fs.createReadStream() creates a stream to read a file piece by piece.
Streams in Node.js process data in:
ASmall chunks
BOne big chunk
COnly text files
DOnly images
✗ Incorrect
Streams handle data in small chunks to save memory and improve speed.
Which is NOT a benefit of using streams?
ALower memory usage
BFaster start of processing
CAutomatic file compression
DAbility to handle large files
✗ Incorrect
Streams do not automatically compress files; they just handle data flow efficiently.
What type of stream can both read and write data?
ADuplex stream
BReadable stream
CWritable stream
DTransform stream
✗ Incorrect
Duplex streams can read and write data, combining both capabilities.
Explain in simple terms why streams are better than loading entire files into memory for large files.
Think about how you eat a big pizza: slice by slice, not all at once.
You got /4 concepts.
Describe how you would use Node.js streams to read a large file and write its content to another file.
Imagine passing water from one bucket to another using a pipe.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using streams in Node.js instead of loading an entire file into memory?
easy
A. Streams load the entire file faster than reading all at once.
B. Streams require less code to read files than other methods.
C. Streams automatically compress files during reading.
D. Streams process data in small chunks, saving memory.
Solution
Step 1: Understand how streams work
Streams read data piece by piece, not all at once, which uses less memory.
Step 2: Compare with loading entire file
Loading entire file reads all data into memory, which can be heavy for big files.
Final Answer:
Streams process data in small chunks, saving memory. -> Option D
Quick Check:
Streams = small chunks, less memory [OK]
Hint: Streams handle data bit by bit, saving memory [OK]
Common Mistakes:
Thinking streams load files faster always
Believing streams compress data automatically
Assuming streams require less code always
2. Which of the following is the correct way to create a readable stream for a file named data.txt in Node.js?
easy
A. const stream = fs.createReadStream('data.txt');
B. const stream = fs.readFile('data.txt');
C. const stream = fs.openStream('data.txt');
D. const stream = fs.streamFile('data.txt');
Solution
Step 1: Recall Node.js stream syntax
The correct method to create a readable stream is fs.createReadStream(filename).
Step 2: Check each option
Only const stream = fs.createReadStream('data.txt'); uses the correct method name and syntax.
Final Answer:
const stream = fs.createReadStream('data.txt'); -> Option A
Quick Check:
Use createReadStream() to read files as streams [OK]
Hint: Use fs.createReadStream() to open file streams [OK]
Common Mistakes:
Using fs.readFile() which reads whole file, not stream
Using non-existent methods like openStream or streamFile
Missing quotes around filename
3. Consider this Node.js code snippet:
const fs = require('fs');
let data = '';
const stream = fs.createReadStream('file.txt');
stream.on('data', chunk => { data += chunk; });
stream.on('end', () => { console.log(data.length); });
What will this code output if file.txt is 5000 bytes?
medium
A. It will print undefined
B. It will print 5000
C. It will print 0
D. It will throw an error
Solution
Step 1: Understand stream data event
The 'data' event adds chunks of the file to the data string as they arrive.
Step 2: Check what happens on 'end'
When the stream ends, data.length is logged, which equals the total bytes read (5000).
Final Answer:
It will print 5000 -> Option B
Quick Check:
Stream chunks combined length = file size [OK]
Hint: Stream 'data' events accumulate full content length [OK]
Common Mistakes:
Assuming data is empty before 'end' event
Expecting undefined because of async nature
Thinking stream throws error without error handler
4. This code tries to read a file using streams but does not print anything: