Examples and Advantages of JavaScript Stream

Examples and Advantages of JavaScript Stream
Techiio-author
Written by Sagar RabidasMarch 11, 2022
6 min read
JavaScript
25 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 25 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss JavaScript Stream

Introduction to JavaScript Stream:-

JavaScript move permits to programmatically get the right of entry to a circulation of facts obtained on the community. Streaming is to interrupt the resource or facts into smaller chunks, and processing is done little by little. In javascript movement, the entire collection is loaded into memory after which processed. For this, we don’t need tons of memory to maintain these bits of facts and their result after the transformation. Previously, while we desired to procedure records along with text or a video, they had to be downloaded and deserialized to a required layout before processing. Now due to javascript circulate, uncooked facts obtained can be at once processed little by little.

Syntax of JavaScript Stream

There is no particular syntax similar to a function or a JavaScript method as JavaScript Stream is an API that allows us to work with the data.

Have 2 types of streaming modes:

  • Reading from Stream: var readStream = fs.createReadStream(<name_of_the_file>)
  • Writing to Stream: var writeStream = fs.createWriteStream(<name_of_the_file>)

Streams in NodeJS are readable and writable, known as Duplex Stream. This is because javaScript streams are Versatile, so these Streams’ main purpose is to reduce the memory needed.

We will be running all the JavaScript applications on the NodeJS platform; NodeJS is written in JavaScript Language, so there would be no change syntactically. Install VSCode and NodeJS to run these below examples.

Examples of JavaScript Stream:-

Given below are the examples of JavaScript Stream:

Example #1

Here we shall see how to create a JS file and read the data from the data.txt file.

Code:

Stream.js:
var fs = require("fs");
var data = '';
var readStream = fs.createReadStream('data.txt');
readStream.setEncoding('UTF8');
readStream.on('data', function(bits) {
data += bits;
});
readStream.on('end',function() {
console.log("Data inside data.txt file is: ", data);
});
readStream.on('error', function(err) {
console.log(err.stack);
});
console.log("JavaScript Program ends here!!");

In data.txt:

Here in EduCBA, you can
learn real world sills
online, at your time,
your place and on any device

Example #2

Here we shall see WritableStream write the given data onto a .txt file.

Code:

var fs = require("fs");
var data = 'eduCBA provides many diversified courses at minimal rate, Friendly instructors and professional courses';
var writeStream = fs.createWriteStream('dataOP.txt');
writeStream.write(data,'UTF8');
writeStream.end();
writeStream.on('finish', function() {
console.log("Write to dataOP.txt file has been completed successfully! Please open dataOP.txt file");
});
writeStream.on('error', function(err){
console.log(err.stack);
});
console.log("Program has been completed");

Example #3

Code:

var fs = require("fs");
var readStream = fs.createReadStream('data.txt');
var writeStream = fs.createWriteStream('dataOP.txt');
readStream.pipe(writeStream);
console.log("Data from data.txt has been copied to dataOP.txt successfully");

Example #4

Code:

var fs = require("fs");
var zlib = require('zlib');
fs.createReadStream('data.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('data.txt.gz'));
console.log("File has been Compressed to Gzip.");

Example #5

Code:

var fs = require("fs");
var zlib = require('zlib');
fs.createReadStream('data.txt.gz')
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream('data.txt'));
console.log("File has been Decompressed to .txt.");

Advantages of JavaScript Stream:-

Given below are the advantages mentioned:

  • Even a beginner with basic knowledge can implement Streams.
  • Ensures timely processing of data reducing unnecessary wastage.
  • Data is not leaked and is fully processed, creating spatial efficiency.
  • Streams are an integral part of programming and help programmers to simplify their code in less time.

Conclusion:-

We have a visible javascript flow and its syntax. The way it works programmatically with some examples on readable stream, writable stream, piping stream, and chaining streams is illustrated. Also, see some of the blessings which are indexed above. Despite such a lot of different environments to be had, javascript streams are one of the reasons why humans had been using NodeJS.

javascript
25 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 25 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance