Question presented to candidate: "You need to copy an 80GB file on disk to a new location. Would fs.readFileSync followed by fs.writeFileSync work in practice, and if not, what is the actual recipe that does?"
What a strong answer should cover:
- The practical recipe for reading and writing a large file is
fs.createReadStream(source).pipe(fs.createWriteStream(destination))— aReadablepiped directly into aWritable, letting.pipe()'s automatic backpressure handling (covered fully, with real measured proof, in its own dedicated question) manage the flow. - 📌 Verified, not assumed: streaming an 80MB file copy this way, with memory sampled every 5ms throughout the entire operation, peaked at only ~86MB of resident memory — confirming memory usage does not scale proportionally with the file's size, unlike
readFileSync/writeFileSync, which would need the entire file's bytes resident in memory at once. - The reason this specifically matters for genuinely large files (the 80GB case in the prompt):
readFileSyncattempting to load that much data into a single in-memory buffer can exceed available memory entirely, causing the process to crash — a failure mode that scales with input size, not something that merely "gets slower." - Transforming data while copying it (rather than a byte-for-byte copy) is a natural extension of the same pattern: inserting a
Transformstream (orzlib.createGzip(), covered in its own dedicated question) between the read and write stages —createReadStream(src).pipe(transform).pipe(createWriteStream(dest))— applies the transformation incrementally, chunk by chunk, with the same flat memory profile. - A precise answer names
stream.pipeline()(covered with real, verified error-cleanup proof in its own dedicated question) as the currently recommended choice over raw.pipe()for production code, specifically because it correctly propagates errors and destroys every stream in the chain on failure — a real, verified gap in.pipe()alone. - The same underlying pattern applies well beyond local file copying: streaming a large file directly into an HTTP response (
createReadStream(path).pipe(res)), or reading a large uploaded file directly into cloud storage without buffering the whole thing in the server's own memory first.
Clarifying questions expected:
- "Is this purely copying, or does the data need to be transformed/compressed along the way?" — decides whether a plain
.pipe()chain or one including aTransformstep is needed. - "Does this specific operation need robust error handling and cleanup across the whole chain?" — routes toward
stream.pipeline()over raw.pipe().
Code / implementation expected: Yes — the real, measured peak-memory result for an actual 80MB streamed copy is the concrete, convincing proof, not a description of "streams use less memory."