Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a buffer in Node.js?

Tags:

node.js

buffer

As you can read in the Node.js documentation on the Buffer class, a buffer

is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.

So far, so good.

What now puzzles me is the question what a buffer is technically speaking. Is it an array with just some additional functions for creating and converting to strings using specific encodings?

Or is there "more" to it?

like image 593
Golo Roden Avatar asked Jan 27 '13 19:01

Golo Roden


People also ask

What is buffer and stream in Nodejs?

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 .

What is buffering in JavaScript?

A buffer is an area of memory. Most JavaScript developers are much less familiar with this concept, compared to programmers using a system programming language (like C, C++, or Go), which interact directly with memory every day.

What is Node JS buffers How do you create it?

Buffers allocate raw memory outside the V8 heap. Buffer class is a global class so it can be used without importing the Buffer module in an application. Creating Buffers: Followings are the different ways to create buffers in Node. js: Create an uninitiated buffer: It creates the uninitiated buffer of given size.

What is no buffering in node JS?

No Buffering − Node. js applications never buffer any data. These applications simply output the data in chunks.


2 Answers

A Buffer is a chunk of memory, just like you would have it in C/C++. You can interpret this memory as an array of integer or floating point numbers of various lengths, or as a binary string. Unlike higher-level data structures like arrays, a buffer is not resizable.

It corresponds roughly to:

  • char* or char[] in C/C++
  • byte[] in Java
  • A mutable bytes or a non-resizable bytearray in Python
  • Strings in php if they were mutable
like image 197
phihag Avatar answered Oct 14 '22 12:10

phihag


BUFFER is a temporary holding spot for data being moved from one place to another.

In order to understand what is Buffer, we need to know how a computer will process things. See the chart below.

The concept is like if you are watching a Youtube Video, you can start to watch a video without downloading the whole video. If your internet speed is too slow, you would see "buffering", that means the computer is trying to collect data in order for you to keep watching that video.

like image 44
Wayne Chiu Avatar answered Oct 14 '22 12:10

Wayne Chiu