What is the differe...
 
Notifications
Clear all

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

  RSS

Sathish Kumar
(@sathish)
Member Moderator
Joined: 4 months ago
Posts: 1203
20/03/2021 10:35 am  

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


Quote
Ganesh A
(@ganesh)
Noble Member
Joined: 4 months ago
Posts: 1170
20/03/2021 10:36 am  
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.

ReplyQuote
Share: