What is the real difference between Fs.readFile() and Fs.createReadSream()?

Asked

Viewed 32 times

2

The point is that I can use both functions to do the same thing. For example:

const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, '../arquivo.txt')

fs.readFile(filePath, 
  (error, fileContent) => console.log(fileContent.toString()))

fs.createReadStream(filePath)
  .on('data', data => console.log(data.toString()))

I know that the createReadStream() returns a stream, but I don’t know exactly all the things a stream can do. The point is I’m using the module fs which I believe is focused on interacting with other files, if I already have two functions to read a file, one asynchronous and one synchronous (.readFile() and .readFileSync()) and return me his content, why do I need another to return me a stream? What will be the advantage in that?

1 answer

2


The function fs.readFile reads the specified file and loads all its contents into a variable. This means that the program will need to allocate enough memory to hold what is contained in the file.

Although, for small files, the effect is the "same" (so that createReadStream may even bring a certain overhead), utilise readFile becomes impracticable when the file size is significantly expressive, as there is not enough memory for the operation.

When you create a stream (such as the createReadStream) does, it is no longer necessary to read the entire file at once. Therefore, smaller (but longer) allocations are required, so that one piece of the file is processed at a time.

In short:

  • Functions such as the readFile tend to be more appropriate for operations on files that contain little content.
  • Functions such as the createReadStream are ideas for dealing with large files, so that allow the processing of the file in "batches".

On the more general concept of Stream, suggest reading this answer or the wikipedia article.


About the readFileSync or readFile, the behavior is the same: load all the contents of the file in the program memory. Streams are a slightly different paradigm from the "classic" concepts of synchronism and asymchronism. They are, nevertheless, closer to the asymchronism, since they do not block the Event loop (as opposed to methods *Sync).

  • I understood but I have almost no idea what would be a large file and a small one, I will give a search in relation to this, thanks for the help you have helped me a lot in my first two questions and it was bad for the bad use of grammar kkkk, but anyway I just wanted to thank you for the help msm.

  • 1

    @arturHamann, imagine a . csv with millions of lines and you want to analyze one by one. Loading it all at once is impractical to the detriment of its size. In this type of situation, it is ideal to use streams. And what grammar, only? I just formatted the function names! :-)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.