Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs streams vs callbacks

Tags:

stream

node.js

Im reading this article: http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/ and having some slight troubles understanding streams.

Quote:

"Suppose we want to develop a simple web application
that reads a particular file from disk and send it to the browser.
The following code shows a very simple and naïve implementation
in order to make this happen."

So the code sample is as follows:

var readStream = fileSystem.createReadStream(filePath);
readStream.on('data', function(data) {
    response.write(data);
});

readStream.on('end', function() {
    response.end();        
});

Why would we use that above way when we could simply do:

fs.readFile(filePath, function(err, data){
  response.write(data);
  response.end();
});

When or why would I use streams?

like image 890
Menztrual Avatar asked Sep 16 '12 02:09

Menztrual


People also ask

Are callbacks faster than promises?

It's a long debated hot topic between javascript developers what is efficient or fast. Till now common perception was that for getting out of callback hell we should use promises or for clean code but what about efficiency. So from my findings i assure you ES6 promises are faster and recommended than old callbacks.

What are Node.js callbacks?

A callback is a function called when the task finishes, and a callback function allows other code to run in the meantime. Using the Callback concept, Node. js can process many requests without waiting for any function to return the result, making Node. js highly scalable.

Is stream an EventEmitter?

All streams are instances of EventEmitter . They emit events that can be used to read and write data. However, we can consume streams data in a simpler way using the pipe method. So this is one of the things that make streams in Node.

Are node streams asynchronous?

The Stream is an instance of the EventEmitter class which handles events asynchronously in Node. Because of this, streams are inherently event-based. To access the stream module: const stream = require('stream');


2 Answers

You'd use stream when working with large files. With a callback, all of the file's contents must be loaded into memory at once, while with a stream, only a chunk of the file is in memory at any given time.

Also, the stream interface is arguably more elegant. Instead of explicitly attaching data, drain, and end callbacks, you can instead use pipe:

var readStream = fileSystem.createReadStream(filePath);
readStream.pipe(response);
like image 128
Xavi Avatar answered Sep 21 '22 23:09

Xavi


One big reason is that you can begin doing work on the data before it is all in memory. Think "streaming video", where you can begin watching a clip while it is still loading. In many use cases, a stream will allow you to begin processing data from a file before you have loaded the entire thing.

The other common use case is when you only want to read an object up until you detect some condition in the data. Say you needed to check to see if a large file contained the word "rabbit". If you use a callback pattern, you will need to read the entire file into memory, then go through the file and check to see whether or not the word is inside. With a stream, you might detect the word on line 5 of the file, then you are able to close the stream, without loading the entire thing.

There are obviously many more complex use cases, and there are still plenty of times where a callback still makes more sense for simplicity (such as if you needed to count the total times "rabbit" appeared, in which case you have to load the entire file anyway).

like image 37
Morgan Herlocker Avatar answered Sep 18 '22 23:09

Morgan Herlocker