"Super-useful node. JS Code Snippet" Serial two: correct splicing buffer

Source: Internet
Author: User
Tags array length

For developers who are beginners of the node. JS Framework, the buffer module may be considered easier to learn and less important. In fact, the buffer module is widely used in file I/O and network I/O, its processing binary performance is much higher than ordinary string performance, the importance is very important. Here we show the reader through a routine that uses the Buf.concat () method for stitching.

The main code for this example ch04.buffer-concat.js is as follows:

1 /** 2 * Ch04.buffer-concat.js3  */  4Console.info ("------Buffer concat vs String concat------"); 5 Console.info (); 6 /** 7 * Define Variable8 * @type {Buffer}9  */  Ten varBUF =NewBuffer ("This is Buffer concat test!");  One varstr ="This is a String concat test!";  A /**  - * Start record time -  */   theConsole.time ("Buffer concat test!");  - varList = [];  - varLen =100000*buf.length;  -  for(varI=0; i<100000; i++){   + List.push (BUF);  -Len + =buf.length;  + }   A /**  at * Buffer Object splicing -  */   - varS1 =buffer.concat (list, Len). toString ();  -Console.timeend ("Buffer concat test!");  - Console.info ();  -Console.time ("string concat test!");  in varList = [];  -  for(varI=100000; i>=0; i--) {   to List.push (str);  + }   - /**  the * String Object Stitching * * @type {string} $  */  Panax Notoginseng varS2 = List.join ("");  -Console.timeend ("string concat test!");  the /**  + * End Record time A  */   the Console.info ();  +Console.info ("------Buffer concat vs String concat------");

"Code Analysis"

Line 10th defines a Buffer object with the variable name buf and initializes a string of data ("Thisis buffer concat test!" The 11th line of code defines a string variable, str, and initializes a string data ("Thisis string concat test!" ), from the beginning of line 15th to the end of line 26th, through the Console.time () and Console.timeend () methods to complete a period of time interval records; the 16th to 21st line defines an array variable list[], The array variable is initialized with the BUF variable, and the 25th line of code is re-stitching the encoding in the list[] array into a buffer object with the Buffer.concat (List,len) method, about Buffer.concat (List,len) The syntax of the method is described below:

Syntax: Buffer.concat (List,[totallength])  

Parameter description:

    • List{array}: Array type, buffer array, used to be connected
    • Totallength:{number} type, first parameter the total size of the buffer array object

The method returns a buffer object that holds all the buffer objects in the buffer array, and if the incoming array has no content, or if the Totallength parameter is 0, it returns a zero-length buffer If there is only one item in the array, the first item is returned, and if there are more than one item in the array, a new instance of the buffer object is created, and if the Totallength parameter is not provided, the read is computed from the buffer array. However, an additional loop is added to calculate the length, so providing a definite totallength parameter will make the Buffer.concat () method execute faster;

As you can see from the results shown in Figure 4.10, stitching using the Buffer.concat (List,len) method is time consuming 48ms.

Figure 4.10 Buffer Object Stitching function

Note: The 2nd parameter of the BUFFER.CONCAT (list, [Totallength]) method is special Totallength, where the totallength is not the array length is the sum of the size of the Buffer instance in the array.


"Super-useful node. JS Code Snippet" Serial two: correct splicing buffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.