What is the difference between readFile vs createReadStream in Node.js?

 readFile — is for asynchronously reads the entire contents of a file. 

It will read the file completely into memory before making it available to the User. readFileSync is synchronous version of readFile.

It is a memory-intensive process and in the case of large files, the processing can be very slow. 

createReadStream — It will read the file in chunks of the default size 64 kb which is specified before hand.

The entire file is split into chunks which are then processed and sent back as a response one by one. 

Once done, they are finally removed from the buffer. 

Unlike readFile, createReadStream is really effective for the processing of the large files.

Comments

Popular posts from this blog

Which node logger did you use in your project?

What are clusters and worker threads, and when would you use them?