a blog for those who code

Wednesday 9 July 2014

readFile Vs createReadStream in Nodejs

In this post we will show you whats the difference between readFile and createReadStream in Nodejs.



readFile - It will read the file completely into memory before making it available to the User.

createReadStream - It will read the file in chunks of the size which is specified before hand.

The problem with readFile is that it will not scale if there are many requests at a time (large data) since it will try to load all of them at that particular time. Instead of that createReadStream can come handy in this situation, which pipe the file contents directly to the HTTP response object. So the user dont have to wait till all the data of the file loaded into memory, he will start receiving the data faster as createReadStream sent out in chunks as it is being read.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment