scarcoco / projx

5 stars 0 forks source link

node-13.x API #10

Open scarcoco opened 4 years ago

scarcoco commented 4 years ago

node.org document

scarcoco commented 4 years ago

Usage

http://nodejs.org/dist/latest-v13.x/docs/api/events.html

import { EventEmitter } from 'events'

const emitter = new EventEmitter()

emitter.on('error', (err) => {
  console.log(err)
})
emitter.on('one', (...args) => {
  console.log('on event -> one(1): ', args)
})
emitter.on('one', (...args) => {
  console.log('on event -> one(2): ', args)
})
// once
emitter.once('two', (...args) => {
  console.log('once event -> two: ', args)
})

emitter.emit('one', 1, 2)
emitter.emit('one', 3, 4)

emitter.emit('two', 5, 6)
emitter.emit('two', 7, 8)
scarcoco commented 4 years ago

Buffer

今天终于认真的把 Buffer 的文档认真看了一下,大致梳理一下。

  1. Buffer 和 字符编码

支持的字符编码方式有 utf8utf16lelatin1,把 Buffer 转变为上诉编码称为解码,反之称为编码。

Node 也支持 hexbase64 两种二进制和字符串互转编码,对于二进制和字符串互转,把 Buffer 转为字符串称为编码,反之称为解码。

另外还有三种遗留的编码方式:asciibinarylatin1)、ucs2utf16le)。

  1. BufferTypedArray 的关系;

Buffer 实例也是 Uint8Array 实例,Uint8ArrayTypedArray 的子类,所以 TypedArray 的方法 Buffer 也有,除了:

BufferTypedArray 互转时,直接使用则会拷贝元素,为了复用内存,可以使用 ArrayBuffer 属性。

When passing a Buffer to a TypedArray constructor, the Buffer’s elements will be copied, interpreted as an array of integers, and not as a byte array of the target type.

In order to create a TypedArray that shares its memory with the Buffer, the underlying ArrayBuffer can be passed to the TypedArray constructor instead:

const buf = Buffer.from('hello', 'utf16le');
const uint16arr = new Uint16Array(
  buf.buffer, buf.byteOffset, buf.length / Uint16Array.BYTES_PER_ELEMENT);

It is also possible to create a new Buffer that shares the same allocated memory as a TypedArray instance by using the TypedArray object’s .buffer property in the same way

const arr = new Uint16Array(2);

arr[0] = 5000;
arr[1] = 4000;

// Copies the contents of `arr`.
const buf1 = Buffer.from(arr);
// Shares memory with `arr`.
const buf2 = Buffer.from(arr.buffer);

console.log(buf1);
// Prints: <Buffer 88 a0>
console.log(buf2);
// Prints: <Buffer 88 13 a0 0f>

arr[1] = 6000;

console.log(buf1);
// Prints: <Buffer 88 a0>
console.log(buf2);
// Prints: <Buffer 88 13 70 17>

it is possible to use only a portion of the underlying ArrayBuffer by passing in byteOffset and length parameters:

const arr = new Uint16Array(20);
const buf = Buffer.from(arr.buffer, 0, 16);

console.log(buf.length);

TypedArray & Buffer 差异如下:

TypedArray.from(source[, mapFn[, thisArg]])

Buffer.from(array)
Buffer.from(buffer)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(string[, encoding])
  1. Bufferiteration

支持 keysvaluesentries 等方法

  1. Buffer.from()Buffer.alloc()Buffer.allocUnsafe()Buffer.allocUnsafeSlow()Buffer.poolSizenew Buffer()new SlowBuffer(size)node --zero-fill-buffers 关系

Buffer.allocUnsafe()

The Buffer module pre-allocates an internal Buffer instance of size Buffer.poolSize that is used as a pool for the fast allocation of new Buffer instances created using Buffer.allocUnsafe() and the deprecated new Buffer(size) constructor only when size is less than or equal to Buffer.poolSize >> 1 (floor of Buffer.poolSize divided by two).

Buffer.allocUnsafeSlow()

However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate amount of time, it may be appropriate to create an un-pooled Buffer instance using Buffer.allocUnsafeSlow() and then copying out the relevant bits

scarcoco commented 4 years ago

Buffer

今天终于认真的把 Buffer 的文档认真看了一下,大致梳理一下。

  1. Buffer 和 字符编码

支持的字符编码方式有 utf8utf16lelatin1,把 Buffer 转变为上诉编码称为解码,反之称为编码。

Node 也支持 hexbase64 两种二进制和字符串互转编码,对于二进制和字符串互转,把 Buffer 转为字符串称为编码,反之称为解码。

另外还有三种遗留的编码方式:asciibinarylatin1)、ucs2utf16le)。

  1. BufferTypedArray 的关系;

Buffer 实例也是 Uint8Array 实例,Uint8ArrayTypedArray 的子类,所以 TypedArray 的方法 Buffer 也有,除了:

BufferTypedArray 互转时,直接使用则会拷贝元素,为了复用内存,可以使用 ArrayBuffer 属性。

When passing a Buffer to a TypedArray constructor, the Buffer’s elements will be copied, interpreted as an array of integers, and not as a byte array of the target type.

In order to create a TypedArray that shares its memory with the Buffer, the underlying ArrayBuffer can be passed to the TypedArray constructor instead:

const buf = Buffer.from('hello', 'utf16le');
const uint16arr = new Uint16Array(
  buf.buffer, buf.byteOffset, buf.length / Uint16Array.BYTES_PER_ELEMENT);

It is also possible to create a new Buffer that shares the same allocated memory as a TypedArray instance by using the TypedArray object’s .buffer property in the same way

const arr = new Uint16Array(2);

arr[0] = 5000;
arr[1] = 4000;

// Copies the contents of `arr`.
const buf1 = Buffer.from(arr);
// Shares memory with `arr`.
const buf2 = Buffer.from(arr.buffer);

console.log(buf1);
// Prints: <Buffer 88 a0>
console.log(buf2);
// Prints: <Buffer 88 13 a0 0f>

arr[1] = 6000;

console.log(buf1);
// Prints: <Buffer 88 a0>
console.log(buf2);
// Prints: <Buffer 88 13 70 17>

it is possible to use only a portion of the underlying ArrayBuffer by passing in byteOffset and length parameters:

const arr = new Uint16Array(20);
const buf = Buffer.from(arr.buffer, 0, 16);

console.log(buf.length);

TypedArray & Buffer 差异如下:

TypedArray.from(source[, mapFn[, thisArg]])

Buffer.from(array)
Buffer.from(buffer)
Buffer.from(arrayBuffer[, byteOffset[, length]])
Buffer.from(string[, encoding])
  1. Bufferiteration

支持 keysvaluesentries 等方法

  1. Buffer.from()Buffer.alloc()Buffer.allocUnsafe()Buffer.allocUnsafeSlow()Buffer.poolSizenew Buffer()new SlowBuffer(size)node --zero-fill-buffers 关系

Buffer.allocUnsafe()

The Buffer module pre-allocates an internal Buffer instance of size Buffer.poolSize that is used as a pool for the fast allocation of new Buffer instances created using Buffer.allocUnsafe() and the deprecated new Buffer(size) constructor only when size is less than or equal to Buffer.poolSize >> 1 (floor of Buffer.poolSize divided by two).

Buffer.allocUnsafeSlow()

However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate amount of time, it may be appropriate to create an un-pooled Buffer instance using Buffer.allocUnsafeSlow() and then copying out the relevant bits