Nodejs Study notes (based on v7.4.0)

Source: Internet
Author: User

Nodejs Study Notes

First, buffer:

Global Objects (Singleton buffer.from buffer.alloc Buffer.allocunsafe)

Encoding type Describe
Ascii Only for 7-bit ascall data encoding, fast, if set up will peel the high
Utf8 Multibyte-encoded Unicode characters, which most of the Web page's documents default to.
Utf16le Small-end encoded Unicode characters, 2 or 4 bytes
Ucs2 Aliases for Utf16le
Base64 Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network
Latin1 One way to encode buffer as a byte encoded string, same as binary
Binary Aliases for Latin1
Hex Encode each byte as two hexadecimal characters


Method parameters Describe Precautions
Buffer.from (Array)

Array normal arrays


Returns a new buffer containing a copy of the provided eight-bit byte

Note that the array must be a number, and if it is a string, it will return a buffer equivalent to the length of the array initialized to 0 (see Example 1)
Buffer.from (arraybuffer[, Byteoffset [, length]])

1, ArrayBuffer: Inherit Typedarry or ArrayBuffer. Buffer

2, Byteoffset: Start copy of the cursor, the default starting from 0

3, Length: To be copied, the default Arraybuffer.length-byteoffset


Returns a new buffer that shares the same allocated memory as the given Arraybuffer

Note: Shared memory, like the shallow copy of JS, shared memory, a change will change (see Example 2)
Buffer.from (buffer)

Buffer: The existing buffer used to copy data from


Returns a new buffer containing a copy of the contents of the given buffer

Note: Each has a different memory (see example 3)
Buffer.from (string[, encoding])

String: Strings to encode

Encoding: Encoding format


Returns a new buffer containing a copy of the supplied string

(see Example 4)

Example 1:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8C/CE/wKiom1h4hyaC42g3AACOcdiuB3M242.png-wh_500x0-wm_ 3-wmp_4-s_1164562943.png "title=" Case 1.png "alt=" Wkiom1h4hyac42g3aacocdiub3m242.png-wh_50 "/>

Example 2:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8C/CE/wKiom1h4klmA2ft5AAB67VR8aD8526.png-wh_500x0-wm_ 3-wmp_4-s_2370072500.png "style=" Float:none "title=" Case 2.png alt= "Wkiom1h4klma2ft5aab67vr8ad8526.png-wh_50"/>

Example 3

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8C/CB/wKioL1h4klmRMWfrAABKj_xz4po226.png-wh_500x0-wm_ 3-wmp_4-s_3853840958.png "style=" Float:none "title=" Case 3.png alt= "Wkiol1h4klmrmwfraabkj_xz4po226.png-wh_50"/>

Example 4:

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8C/CF/wKiom1h4lbvx5suJAABMqoTszb4035.png-wh_500x0-wm_ 3-wmp_4-s_3433459901.png "title=" Case 4.png "alt=" Wkiom1h4lbvx5sujaabmqotszb4035.png-wh_50 "/>

Method parameters Describe Precautions

Buffer.alloc (size[, fill[, encoding])

Size: Sizes of new buffer

Fill: Initialized data

Encoding: Encoding format


Returns the new buffer

Compared to Allocunsafe, it is slower but ensures that no dirty data or initialized data is available

(see example 5)

Buffer.allocunsafe (size)

Size: Sizes of new buffer


Returns a new buffer that shares the same allocated memory as the given Arraybuffer

Compared to alloc, it is faster, but with dirty data and does not initialize

(see example 5)

Case 5:

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8C/CF/wKiom1h4nZ_AWGFBAABb9DZssEY593.png-wh_500x0-wm_ 3-wmp_4-s_3271290673.png "title=" Case 5.png "alt=" Wkiom1h4nz_awgfbaabb9dzssey593.png-wh_50 "/>



This article is from the "Do Your torrent" blog, please be sure to keep this source http://blackrain.blog.51cto.com/10622094/1891864

Nodejs Study notes (based on v7.4.0)

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.