This article is for reading Pauling's big "in the text node. js" Note:
In front of the development of the time, we have not used buffer, and no use. Buffer is introduced in the node environment to facilitate processing of binary data. Here we should have a basic understanding of it and correct positioning, to better use it.
1. Buffer does not belong to the content contained in V8
Buffer is implemented in C + + by node, similar to some of the other core modules, and does not mean to modify V8 so that this function is implemented in V8.
2, buffer does not need require
We use modules such as the file module and so on need require to introduce the corresponding module, but buffer does not need, the main reason is because it is too common, so at the time of loading, the system will load the buffer on the global buffer on the default, So you can use it directly without require.
3. The memory used to store data in buffer is not V8 and is not affected by V8 memory limit
Buffer occupies two parts of memory, part of the buffer object, and a portion of the pool used to store the data. The new buffer object is an object belonging to the V8 memory, which, like the other variables of the V8, satisfies the mechanism of automatic garbage collection. The pool used to store the data is stored by C + + directly to the system, and has its own memory processing mechanism. Therefore, the memory of the buffer data store is outside the data V8 and will not be affected by the V8 maximum memory limit. The corresponding Buffer object holds the location information and the size information of the pool.
4, buffer and string can be easily converted to each other, but they are different
A) buffer also has length, but buffer is mainly related to the number of bytes stored in the data, but the string is related to the corresponding encoding.
b) Because buffer is actually not directly related to the corresponding code, you can even store multiple encoded content in a buffer, as long as you can remove the corresponding content by the location.
c) The usual data flow operation, the use of the buffer is actually used to facilitate the binary transmission of data, and the string is mainly used in the engine string operation.
Common handling techniques for buffer:
1, we often in the case of reading a data stream in the form of a chunks+=chunk to the data stream the information returned by the event splicing together:
var rs=Createreadstream (); var chunks= '; Rs.on (' data ',function(chunk) { chunks+ =chunk;}); Rs.on (' end ',function() { console.log (chunks);});
The main problem here is that the data event incoming chunk is actually a buffer data, his content may be truncated, not a complete string, and later use the
Chunks+=chunk, in effect, turns the buffer into a string and then splicing. If the buffer is truncated, it can cause the string to be recognized as garbled.
So this should be done using the buffer direct concatenation for the completed buffer form to solve.
var rs=Createreadstream (); var chunks=[];rs.on (' data ',function(chunk) { chunks.push (chunk);}); Rs.on (' end ',function() { console.log (Buffer.concat (chunks));});
2. We may have a long string that needs to be returned to the user each time the user requests it, such as:
var longstr= '; for (var i=0;i<10*1024;i++) { longstr+ = ' a ';} // longstr=new Buffer (LONGSTR); Http.createserver (function(req,res) { res.writehead (); Res.end (LONGSTR);});
The problem in this case is that every time the request calls Res.end, it is necessary to do an operation equivalent longstr=new Buffer (LONGSTR);
So it's time to move this step forward. Remove the comments in the code so that we can store the original longstr directly as buffer, and follow it directly, without wasting the CPU.
This article is to read the reading notes of Pauling. js
Some points that buffer needs to know in node. js