node.jsstreambufferfile-access

In node, is a readable file stream just a buffered, asynchronous read "under the hood"?


I'm wondering, if I were to dive into the stream code, would I find deep down that for a readable file stream what is basically happening is that data is being asynchronously read into a buffer which is then emitted in a data event?

Asked another way, if my intention is simply to read data from a file without storing all of it in memory, and it doesn't need to be piped anywhere, what is the benefit of creating a read stream versus doing sequential asynchronous reads into a buffer?

Of course, I could dive into the code myself, but I'm thinking that someone may already know the answer ;)


Solution

  • Yes, a readstream is just a buffer, asynchronous reads, an event system and a bunch of useful APIs around all that for some of the things you might want to do with the data.

    If you don't need any of that, you can just do your own asynchronous reads just fine.