Correct stitching of Node.js practical code segment Buffer_node.js

Source: Internet
Author: User
Tags time interval

For beginners Node.js Framework developers, may think that the buffer module is easier to learn, the importance is not so prominent. In fact, the buffer module in file I/O and network I/O application is very wide, its processing binary performance than ordinary string performance is much higher, the importance is important. Let's use a routine to show the reader the process of stitching using the Buf.concat () method.

This example ch04.buffer-concat.js the main code as follows:

/** 
 * ch04.buffer-concat.js * * 
console.info ("------  buffer concat vs String concat------"); 
Console.info (); 
/** 
 * Define variable 
 * @type {buffer} 
/var buf = new Buffer ("This is buffer concat test!"); 
var str = "This is String concat test!"; 
/** 
 * Start record time 
 * 
/Console.time ("Buffer concat test!"); 
var list = []; 
var len = 100000 * buf.length; 
for (var i=0; i<100000; i++) { 
  list.push (BUF); 
  Len + + buf.length; 
} 
/** 
 * Buffer Object stitching 
 / 
var s1 = buffer.concat (list, Len). toString (); 
Console.timeend ("Buffer concat test!"); 
Console.info (); 
Console.time ("string concat test!"); 
var list = []; 
for (var i=100000; i>=0; i--) { 
  list.push (str); 
} 
/** 
 * String Object stitching 
 * @type {string} 
 * 
/var s2 = list.join (""); 
Console.timeend ("string concat test!"); 
/** * End Record time * 
/Console.info (); 
Console.info ("------  Buffer concat vs String concat------");

"Code Analysis"

The 10th line of code defines a buffer object, the variable named buf, and initializes a string 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 line 15th to line 26th, the Console.time () and Console.timeend () methods are used to complete a period of time interval records; the 16th to 21st line of code defines an array variable list[], The array variable is initialized with the BUF variable, and the 25th line of code in the list[] array is spliced back into a buffer object by the Buffer.concat (List,len) method, about Buffer.concat (List,len) The syntax for 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 buffer the total size of the array object

The method returns a buffer object that holds the concatenation of all the buffer objects in the incoming buffer array, if the passed-in 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; If the totallength argument is not supplied, the read is computed from the buffer array. However, an additional loop is added to compute the length, so providing a clear totallength parameter will make the Buffer.concat () method perform faster;

The results shown in the diagram show that the Buffer.concat (List,len) method for stitching is 48ms.

Buffer Object Splicing function

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

The above is the second practical node.js Code section, I hope to help you learn.

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.