Node API buffer

Source: Internet
Author: User

https://cnodejs.org/topic/5189ff4f63e9f8a54207f60c

1. When stitching strings, string is faster than buffer and buffer needs ToString (). Buffer is required when saving non-utf-8 strings, 2 binary and other formats.

var string3, Buffer3;console.time (' Write 1024*1024*10 string '), for (Var j=0; j<1024*1024*10; J + +) {    var x = j+ '; C1/>string3 + = x;} Console.timeend (' Write 1024*1024*10 string '); Console.time (' Write 1024*1024*10 buffer '); buffer3 = new Buffer (1024*1024 *10); for (var j=0; j<1024*1024*10; J + +) {    var x = j+ ';    Buffer3.write (x, j);} Console.timeend (' Write 1024*1024*10 buffer ');
Note how the console is written, and the creation and splicing of the buffer.

2, buffer 8KB will cause memory leaks

When we instantiate a new buffer class, according to the size of the instantiation to apply for memory space, if the required space is less than 8KB, it will be more time to determine whether the current 8KB carrier remaining capacity is enough new buffer instance, if enough, The new buffer instance is saved in the current 8KB vector and the remaining space is updated. If not enough, the new application is a 8KB, and as the current carrier.

3, Buffer splicing method performance Comparison

var buf = new Buffer (' sasdasd '); Console.time (' string + = Buf '); var s = '; for (var j=0; j<100000; J + +) {    S + = buf;
   
    }console.log (' s.length: ' +s.length); Console.timeend (' string + = Buf '); Console.time (' buf concat '); var list = [];var len = 0;for (var j=0; j<100000; J + +) {    List.push (BUF);    Len + = Buf.length;} var s2 = buffer.concat (list, Len). toString (); Console.log (' S2.length: ' +s2.length '); Console.timeend (' buf concat ');
    
The latter must be fast

At this point, I was out of my mind, trying to compare the second method of a 1 with the second of 3, which is faster:

var buf = new buffer (' SASDASD '), Console.time (' Write 1024*1024*10 buffer '), var buffer3 = new buffer (700000); for (var j=0; j& lt;100000; J + +) {    var x = ' sasdasd ';    Buffer3.write (x, j);} Console.log (' buffer3.length: ' +buffer3.length); Console.timeend (' Write 1024*1024*10 buffer '); Console.time (' Buf Concat '); var list = [];var len = 0;for (var j=0; j<100000; J + +) {    List.push (buf)    ; Len + = Buf.length;} var s2 = buffer.concat (list, Len). toString (); Console.log (' S2.length: ' +s2.length '); Console.timeend (' buf concat ');

The wording changes somewhat, but the latter one is faster.

Next talk about + = more prone to errors: according to the "easy to follow node. js"

1, data + + chunk; Equivalent to data.tostring () = data.tostring () + chunk.tostring ();

2, toString () default Utf-8 encoding, in which case Chinese is 3 bytes, Chinese is wide-byte encoding, chunk may truncate the Chinese string, causing some bytes to appear garbled.

Node API 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.