Table of contents
No headings in the article.
"Hey there, kiddo! Do you like to play with toys that pour water or sand from one container to another or you would love to play with lego bricks? Well, imagine that but instead of water, sand or lego bricks, we are playing with data! And Node.js has special tools to help us play this game, called streams and buffers."
Technically, Streams and buffers are core concepts in Node.js, which is a platform built on top of Google's V8 JavaScript engine. Streams are a way of processing and handling data in real time, while buffers are a way of storing data temporarily in memory.
A stream is a continuous flow of data, similar to a river or a stream of water. In Node.js, streams can be used to process large amounts of data, such as reading a file or reading data from the internet, without having to load all the data into memory. This makes streams a very efficient way to process and handle data.
Buffers, on the other hand, are a way of storing data temporarily in memory. They are similar to an array and are used to store data temporarily before it is processed. The advantage of buffers is that they can store any type of data, not just text. This means that they are very flexible and can be used in many different situations.
Well, I know this all sounds very overwhelming ๐ฐ
Your immediate next question to me will be, Shekhar, you were about to explain streams and Buffers as we were of age 5?
Well, I've got you covered, I understand that you may have found the initial technical definitions confusing, but now let me explain the same topic in a way that you can easily relate to and understand it better."
So, imagine you are building a big tower with blocks. Every block represents some data, and you want to move those blocks from one place to another. Looking at the above example image You can think of the tower of lego blocks as a large amount of data that needs to be transferred from one place to another, just like you need to move the tower from bedroom 1 to bedroom 2. Instead of trying to move the entire tower at once, you can use streams to move the tower piece by piece, similar to how streams in Node.js allow you to process data in small chunks. That's what "streams" and "buffers" in Node.js are all about!
Streams are like a flow of water. They allow you to move data from one place to another but in small pieces. For example, when you drink a glass of water, you don't drink the whole glass at once. You take small sips. Similarly, streams allow you to take small pieces of data and move them from one place to another.
Buffers, on the other hand, are like box containers. They are used to store data temporarily. For example, look at the above image. Let's say you are very talented at building towers with lego bricks, your school has invited you for interschool lego tower building competition. So the immediate next thing you do is pack all your lego bricks inside a box container so that you can just load them onto your school bus and take them off with you for the competition.
But the condition is, a specific box container can hold a limited amount of lego bricks. Now, each lego block is like a small piece of data that you want to store and build something with it in your competition. So once you take to fill all your lego bricks inside multiple containers your all containers are now filled temporarily for your competition. Now at your school you can use your lego bricks as your need from those containers boxes.
In a computer, data is stored in a similar way, as small pieces of information that need to be put together to form something meaningful. This is where buffers come in. A buffer is like a virtual box where you can store these small pieces of data temporarily before you use it to build something else.
In nodejs, buffers are used to store data in memory so that it can be efficiently processed and transmitted. Just like how you would pick lego blocks from the box and place them in a specific order to build your tower, nodejs can pick up these small pieces of data from the buffer and use it to perform some operations.
So, when you use streams and buffers in Node.js, you can take small pieces of data and store them temporarily in a buffer. Then, you can move that data from one place to another using streams. This helps make your application faster and more efficient.
I hope this helps explain streams and buffers in a way that's easy to understand!
Thank you so much for reading. Do Follow and Subscribe to the Newsletter (Blog) to get notified when I post.