How to handle large files efficiently in Node.js
· Category: Node.js
Short answer
Use streams and pipelines to process large files chunk by chunk, avoiding loading entire files into memory.
Steps
- Use
fs.createReadStream()to read files incrementally. - Pipe through transform streams like
zlib.createGzip()for compression. - Write to the destination with
fs.createWriteStream(). - Use
stream.pipeline()to handle errors and cleanup automatically. - Set an appropriate
highWaterMarkto balance memory use and I/O performance.
Tips
- For CSV processing, use streaming parsers like
csv-parserinstead of loading the whole file into an array. - Monitor memory usage with
--max-old-space-sizeflags if you must buffer portions of the file.
Common issues
- Buffering entire large files causes
JavaScript heap out of memoryerrors. - Synchronous methods like
readFileSyncblock the event loop, freezing other requests until the operation completes.