Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Last active November 19, 2022 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirilkirkov/7669d8c26990c9c40eb85c961933ab6a to your computer and use it in GitHub Desktop.
Save kirilkirkov/7669d8c26990c9c40eb85c961933ab6a to your computer and use it in GitHub Desktop.
When to use streams and when buffer - Usage in NodeJS

Whats the difference between Streams and Buffer?

  • Buffer is temporary placeholder in memory (ram/disk) on which data can be dumped and then processing can be done.
  • Stream is a sequence of data elements. like when you typing something in your computer then when you press the keys it will form a data stream and then it goes to the processor for processing.

Stream can be processed through Buffer.

What are buffers and streams in REST API?

A buffer is a temporary memory that a stream takes to hold some data until it is consumed. In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes. A buffer memory in Node by default works on String and Buffer

Is stream better than buffer?

So what is the difference between Stream & Buffer? A buffer has a specified, definite length whereas a stream does not. A stream is a sequence of bytes that is read and/or written to, while a buffer is a sequence of bytes that is stored.

Streams and Buffers in Node.js

To handle and manipulate streaming data like a video, a large file, etc., we need streams in Node. The streams module in Node.js manages all streams.

Node.js streams extend the EventEmitter class. We can listen to events like data and end in streams.

To simply listen to an event, we need to use the stream.on() function available in the stream.

Node.js Stream readable.pipe() Method

The readable.pipe() method in a Readable Stream is used to attach a Writable stream to the readable stream so that it consequently switches into flowing mode and then pushes all the data that it has to the attached Writable. 

Return Value: It returns the stream.Writable destination, allowing for a chain of pipes if it is a Duplex or a Transform stream. Below examples illustrate the use of readable.pipe() method in Node.js: 

// Node.js program to demonstrate the    
// readable.pipe() method
  
// Accessing fs module
var fs = require("fs");
 
// Create a readable stream
var readable = fs.createReadStream('input.txt');
 
// Create a writable stream
var writable = fs.createWriteStream('output.txt');
 
// Calling pipe method
readable.pipe(writable);
 
console.log("Program Ended");

So, after the piping method the file named “output.text” must contain the data that was in the file “input.text”. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment