JavaScript is very friendly to string processing, whether it is a wide-byte or single-byte string, and is considered a string. Node needs to deal with network protocols, manipulate databases, process pictures, file uploads, and so on, and also deal with a large number of binary data, the resulting string is far from meeting these requirements, so buffer was born.
Buffer structure
Buffer is a typical JavaScript and C + + module, performance-related parts are implemented in C + +, non-performance-related parts are implemented with JavaScript.
When node starts the process, buffer is loaded into memory and placed into the global object, so there is no need to require
Buffer object: Similar to an array whose elements are two-digit 16 binary digits.
Buffer memory allocation
The memory allocation of the buffer object is not in the heap memory of V8, which implements the memory request at node's C + + level.
In order to efficiently use applications for memory, node uses slab allocation mechanism, slab is a dynamic memory management mechanism, application of various *nix operating systems. There are three states of slab:
(1) Full: Fully allocated status
(2) Partial: Partial distribution status
(3) Empty: not Assigned status
The buffer conversion Buffer object can be converted to and from the string, supported by the following types of encodings: ASCII, UTF-8, Utf-16le/ucs-2, Base64, Binary, Hex
string to bufferNew Buffer (str, [encoding]), default Utf-8buf.write (string, [offset], [length], [encoding])
buffer to StringBuf.tostring ([encoding], [start], [end])
type of encoding not supported by bufferBuffer.isencoding (encoding) to determine if Iconv-lite is supported: pure JavaScript implementation, lighter, better performance without C + + to JavaScript conversion iconv: calling C + + The Libiconv library completes the splicing of the buffer note res.on (' Data ', function (chunk) {}), where the parameter chunk is the buffer object, directly with the + stitching will be automatically converted to a string, for wide-byte characters may cause garbled production, Workaround (1) through the Setencoding () method in a readable stream, this method allows the data event to be passed as a buffer object instead of the encoded string, which uses the Stringencoder module internally. (2) The buffer object is staged into the array, and finally assembled into a large buffer after the encoding converted to a string output buffer in the file I/O and network I/O is widely used, its performance is significant, more than ordinary string performance is much higher. The use of buffer in addition to the conversion of the string has a performance loss, when the file is read, there is a highwatermark setting is critical to the performance impact. A. Highwatermark settings have a certain effect on buffer memory allocation and use B. Highwatermark settings are too small and may cause too many system calls