Open scarcoco opened 4 years ago
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)
今天终于认真的把 Buffer
的文档认真看了一下,大致梳理一下。
Buffer
和 字符编码支持的字符编码方式有 utf8
、utf16le
、latin1
,把 Buffer
转变为上诉编码称为解码,反之称为编码。
Node 也支持 hex
和 base64
两种二进制和字符串互转编码,对于二进制和字符串互转,把 Buffer
转为字符串称为编码,反之称为解码。
另外还有三种遗留的编码方式:ascii
、binary
(latin1
)、ucs2
(utf16le
)。
Buffer
和 TypedArray
的关系;Buffer
实例也是 Uint8Array
实例,Uint8Array
是 TypedArray
的子类,所以 TypedArray
的方法 Buffer
也有,除了:
TypedArray#slice()
create a copy of TypedArray,but Buffer#slice()
only create a view over original Buffer;TypedArray#subarray()
的效果和 Buffer#slice
效果一致;buffer.toString()
和它对应的 TypedArray
的 toString
不兼容;indexOf
等方法支持更多参数;Buffer
和 TypedArray
互转时,直接使用则会拷贝元素,为了复用内存,可以使用 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])
Buffer
和 iteration
支持 keys
、values
、entries
等方法
Buffer.from()
、Buffer.alloc()
、Buffer.allocUnsafe()
、Buffer.allocUnsafeSlow()
、Buffer.poolSize
、new Buffer()
、new SlowBuffer(size)
、node --zero-fill-buffers
关系new Buffer()
和 new SlowBuffer(size)
已经废弃,具体可以查看文档。Buffer.from()
用法如上;Buffer.alloc()
、Buffer.allocUnsafe()
、Buffer.allocUnsafeSlow()
、Buffer.poolSize
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).
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
今天终于认真的把 Buffer
的文档认真看了一下,大致梳理一下。
Buffer
和 字符编码支持的字符编码方式有 utf8
、utf16le
、latin1
,把 Buffer
转变为上诉编码称为解码,反之称为编码。
Node 也支持 hex
和 base64
两种二进制和字符串互转编码,对于二进制和字符串互转,把 Buffer
转为字符串称为编码,反之称为解码。
另外还有三种遗留的编码方式:ascii
、binary
(latin1
)、ucs2
(utf16le
)。
Buffer
和 TypedArray
的关系;Buffer
实例也是 Uint8Array
实例,Uint8Array
是 TypedArray
的子类,所以 TypedArray
的方法 Buffer
也有,除了:
TypedArray#slice()
create a copy of TypedArray,but Buffer#slice()
only create a view over original Buffer;TypedArray#subarray()
的效果和 Buffer#slice
效果一致;buffer.toString()
和它对应的 TypedArray
的 toString
不兼容;indexOf
等方法支持更多参数;Buffer
和 TypedArray
互转时,直接使用则会拷贝元素,为了复用内存,可以使用 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])
Buffer
和 iteration
支持 keys
、values
、entries
等方法
Buffer.from()
、Buffer.alloc()
、Buffer.allocUnsafe()
、Buffer.allocUnsafeSlow()
、Buffer.poolSize
、new Buffer()
、new SlowBuffer(size)
、node --zero-fill-buffers
关系new Buffer()
和 new SlowBuffer(size)
已经废弃;Buffer.from()
用法如上;Buffer.alloc()
、Buffer.allocUnsafe()
、Buffer.allocUnsafeSlow()
、Buffer.poolSize
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).
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
node.org document