Buffer
- What is buffer
- Why is buffer needed
- Character Encoding
- How to use it?
What is buffer?
Buffer is kind of confusing because the word represents 2 definition in Node.js.
Buffer
"module" is a module for reading or writing streams of binary data.Buffer itself is a temporary holding spot (in usually RAM) for data being moved from one place to another. Node.js buffers correspond to fixed-sized, raw memory allocations outside the V8 heap.
When you use buffer module, it creates temporary data spot and sending binary data. Buffer can be streamed which means chunked and send data even if all data is not read. Sometime video streaming says "Buffering" during loading time and it means collecting decent amount of data from stream so that you can watch video for 10 sec or 20 sec at least without freezing.
Pure JavaScript is Unicode friendly, but it is not so for binary data. While dealing with TCP streams or the file system, it's necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.
Buffer class is a global class that can be accessed in an application without importing the buffer module.
https://stackoverflow.com/questions/14551006/what-is-a-buffer-in-node-js
Why is buffer needed?
- Handling file data
- Need to be streamed (chunk file) such as file system operations
For example
- File operation (File might not contain string but binary data & if it is large size, it might need to be chunked)
Character Encoding
Character set represents the number of character.
What is encoding?
encoding is transforming string to another format. When it comes to Character Encoding, it is all about changing character to binary data. You get different result when you use UTF-8 and ASCII.
These encoding is available on Buffer object.
https://nodejs.org/api/buffer.html
Encoding | |
---|---|
UTF-8 | UTF-8 is a variable width character encoding capable of encoding all 1,112,064 valid code points in Unicode using one to four 8-bit bytes. |
Unicode | 136,755 characters covering 139 modern and historic scripts. |
ASCII | ASCII (American Standard Code for Information Interchange) is the most common format for text files in computers and on the Internet. In an ASCII file, each alphabetic, numeric, or special character is represented with a 7-bit binary number (a string of seven 0s or 1s). 128 possible characters are defined. |
base64 | Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. |
https://www.w3schools.com/html/html_charset.asp
How to use it?
alloc | |
---|---|
allocUnsafe | |
from | |
Buffer.alloc(10);
Buffer.from("A");
new Buffer("a");
What’s the difference betweenBuffer.alloc
andBuffer.allocUnsafe
?
Buffer.alloc
allocates a memory chunk, initializes it (sets every cell to either zero or some predefined value) and returns a Node.js Buffer wrapping this memory chunk.
Buffer.allocUnsafe
skips the initialization stage. Instead it returns a Buffer pointing to uninitialized memory. This reduces the allocation time duration, but creates a possibility for (sensitive) data leakage, if this uninitialized memory is exposed to the user. Thus, you should only
Buffer.allocUnsafe
only if you plan to initialize the memory chunk yourself.
What is thestring_decoder
module useful for? How is it different than casting buffers to strings?
Thestring_decoder
is useful for decoding aBuffer
instance into strings while preserving endoded multi-byte UTF-8 / UTF-16 characters. In oppose to simple buffer cast to string, thestring_decodure
can detect incomplete multibyte characters and handle them. For example: