On the other hand in most cases you want to read / write through the data once and in one direction where streams will be helpful. Streams return smaller parts of the data and trigger a callback when new data is available for processing.
For an input operation, buffer mode will wait until all the data is collected into a buffer and then pass it to a callback as soon as the entire resource is read. In the below image you can see that when some data are received it is saved into the buffer. But when everything is received i.e. the final one which causes the entire buffer to be sent to the customer.
Whereas stream will allow you to process the data as soon as it arrives from the resource. In the below image each new chunk of data is received from the resource is immediately provided to the consumer, thus consumer can process the data straightaway without waiting for all the data to be collected in the buffer.
There are two main differences between Streams and Buffers and they are :
1. Space Efficiency - Lets say you need to read a very big file, so if you use buffer, you need to read the file completely before processing which is not a good idea because you will soon run out of memory. Thus for that you need to use streams.
2. Time Efficiency - Lets say you need to compress a file and upload it to the server which will decompress the file. So if you use buffer it will read the entire data and then compress it similarly decompression on the server will start only when the data has been received. So to minimize the time you can use streams which will compress small data chunks and send to the server. Server will also decompress small chunks of data rather than full buffer.
Please Like and Share CodingDefined.com Blog, if you find it interesting and helpful.