Correct splicing of Node. js practical code segments Buffer, node. jsbuffer
Developers who are new to the Node. js framework may think that the Buffer module is easier to learn and is not so important. In fact, the Buffer module is widely used in file I/O and network I/O, and its binary processing performance is much higher than that of common strings, so it is important. Next we will use a routine to demonstrate the process of splicing using the buf. concat () method.
In this example, the main code of ch04.buffer-concat. js is 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 splicing */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 splicing * @ 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 Code in line 10th defines a Buffer object named buf and initializes a string data ("thisis Buffer concat test! "); Line 4 defines a String variable str and initializes a String data (" thisis String concat test! "); From 15th lines of code to 26th lines of code, the interval record is completed through the console. time () and console. timeEnd () methods; 16th ~ Line 21 defines an array Variable list [] and uses the buf variable to initialize the array variable. Line 21 uses the Buffer. the concat (list, len) method reassembles the encoding in the list [] array into a Buffer object. the concat (list, len) method syntax is described as follows:
Syntax:Buffer. concat (list, [totalLength])
Parameter description:
List {Array }:Array type, Buffer array, used for connection
TotalLength :{Number} type, the total size of the first Buffer array object
This method returns a buffer object that stores all the buffer objects in the input buffer array. If the input array has no content, or the totalLength parameter is 0, A zero-length buffer is returned. If there is only one entry in the array, the first entry is returned. If there are more than one entry in the array, A new Buffer object instance will be created. If the totalLength parameter is not provided, the length will be calculated and read from the buffer array, but an additional loop will be added to calculate the length, therefore, providing a clear totalLength parameter will make the Buffer. the concat () method is executed faster;
The displayed result shows that the splicing time using the Buffer. concat (list, len) method is 48 ms.
Buffer object splicing Function
Note:The totalLength parameter of the Buffer. concat (list, [totalLength]) method is special. Here, totalLength is not the array length, but the total size of the Buffer instance in the array.
The above is the second practical Node. js code segment, hoping to help you learn.
Articles you may be interested in:
- Node. js: A simple page output implementation code
- Node. js regular expression to get code instances of all links on the webpage
- Node. js implements code for reading file content line by line
- My path to Node. js Learning (iii) -- Functions of node. js, callback, synchronous and asynchronous code, and event Loops
- Detailed description of binary data encoding and decoding using Buffer in Node. js
- How to Use the buffer. write method in node. js
- How to Use the buffer. toString method in node. js
- How to Use buffer. toJSON in node. js
- How to Use the buffer. copy method in node. js