What is the difference between readFile vs. createReadStream in Node.js?
Pure Pipes | Impure Pipes |
---|---|
This will thoroughly read the file into the memory before making it available to the user. | It will read chunks of a file as per specifications provided by the user. |
Since the whole data is sent after it has been loaded, it will take time for the client to reach and hence, is slower. | Since it reads files in chunks, the client will read the data faster than in readFile. |
It is easier to clean the non-used memory by Node.js in this. | It is much more difficult for Node.js to clean up memory in this case. |
It will not scale the requests at a given time, preferably all at once. | It will pipe the content directly to the client using HTTP response objects, making it time-saving. |
Note: Our aim while creating nodejs interview questions, is to help you grow as a Node Developer. The questions mentioned here have been asked by leading organizations during technical rounds of the interview process.
BY Best Interview Question ON 21 Feb 2021