DEV Community

LG
LG

Posted on • Edited on

Node.js fundamentals allegorically

Stream vs. Buffer

Stream is an abstract interface (“abstract” meaning it made for beginner users i.e. relatively easy-to-use) primarily to work with streaming (which means “in-memory”) (session) data rather than in-disk (persistence-like) data .

EXAMPLE : Pipe is an important mechanism connecting two & more processes (streams, processes are mounted onto PCB, RAM by kernel) without storing anything on the disk for persistency [backed up by baeldung] ;

Readable – earth , that generates water ;

Stream (continues in time) FIFO-like Buffer (discrete in time) STACK-like
Example: Constantly running water in your mouth (consumer, writable) in small gulps (chunks) Example: Water as ice cubes, the measured units of water to prevent clogging
How long can you drink without taking a breath (discrete pause of time) Buffer as stream optimization gives discrete pauses between "swallowing" big ice cube chunks (blobs) by one's mouth as a writable (consumable)

NOTE : Stream can be readable, but not mandatory writable – water can be generated, but not consumed;

KEEP IN MIND : Buffer does pack stream in discrete units, giving a measuring feature, that sounds like optimization, however streaming ensures plausibly un-interrupted consumption . Inter-process-communication (IPC) messaging is a real life example of buffering, whereas I/O over Network (e.g. video, audio, or both) is a real life example of streaming .


Refs

  • Node.js fundamentals et al. link

Stay tuned, more is coming shortly !

Top comments (0)