蓝色天空.知识 · 实践 · 创造
我的收藏
Node.js 教程目录 38章
Node.js / LEARN BY DOING

Node.js Buffer(缓冲区)

核对 Node.js Buffer 固定字节二进制缓冲、字符编码转换、安全创建 API、读写/JSON/合并/比较/拷贝/切片操作及 49 项二进制参考方法。

学完这一章,你能够

  • 核对 Node.js Buffer 固定字节二进制缓冲、字符编码转换、安全创建 API、读写/JSON/合并/比较/拷贝/切片操作及 49 项二进制参考方法。

Buffer 定位、特性与字符编码转换

早期 JavaScript 仅内置字符串类型而缺乏原生二进制缓冲支持,Node.js 引入 `Buffer` 类用于高效处理文件流、网络数据包与图像等原始字节序列。`Buffer` 本质是固定字节长度的 8 位无符号整数数组(现为 `Uint8Array` 子类):分配后单字节内容可原地修改,但总长度固定不可动态伸缩。

借助显式字符编码可在 `Buffer` 与普通字符串间互转。Node.js 常用编码包括 `ascii`(7 位 ASCII)、`utf8`(多字节 Unicode,默认编码)、`utf16le`(2 或 4 字节小端 Unicode,支持代理对 U+10000~U+10FFFF)及其别名 `ucs2`、`base64`、`latin1`(单字节编码)及其别名 `binary`,以及每字节映射两位十六进制字符的 `hex`。本站 Node.js v24.22.3 实测 `Buffer.from('node24', 'ascii')` 转 `hex` 为 `'6e6f64653234'`、转 `base64` 为 `'bm9kZTI0'`;字符 `'tést'` 在 `utf8` 下占 5 字节 `[116, 195, 169, 115, 116]`,在 `latin1` 下占 4 字节 `[116, 233, 115, 116]`。

创建 Buffer 实例与安全分配原则

现代 Node.js 使用静态工厂方法创建 `Buffer`(旧 `new Buffer()` 已废弃):`Buffer.alloc(size[, fill[, encoding]])` 分配并清零或按指定值填充;`Buffer.allocUnsafe(size)` 与非池化的 `Buffer.allocUnsafeSlow(size)` 跳过初始化速度更快,但可能残留旧内存数据,使用前须通过 `fill()` 或 `write()` 覆盖;`Buffer.from(array)` 按字节数组初始化(非数字元素按 0 处理);`Buffer.from(arrayBuffer[, byteOffset[, length]])` 与底层 `ArrayBuffer` 共享同一块内存(实测修改 `viewFromAb[0] = 0x41` 同步反映到 `Uint8Array` 视图为 `65`);`Buffer.from(buffer)` 执行深拷贝互不影响;`Buffer.from(string[, encoding])` 按编码转换字符串。

读写、长度、JSON 序列化与常用字节操作

`buf.write(string[, offset[, length]][, encoding])` 按指定编码从 `offset`(默认 0)起写入最多 `length`(默认至末尾)字节,返回实际写入的字节数,并在空间不足时拒绝截断半个多字节字符。实测在 256 字节零缓冲写入 `'mbsky-node-buf'` 返回 `14`,在仅 5 字节缓冲写入 4 个中文字符 `'蓝色天空'`(UTF-8 共 12 字节)时仅完整写入 3 字节 `'蓝'`。

读取使用 `buf.toString([encoding[, start[, end]]])`(默认 `utf8`、`start=0`、`end=buf.length`):实测写入 ASCII 97~122 后完整解码为 `'abcdefghijklmnopqrstuvwxyz'`,截取 `[0, 5)` 在 `ascii`/`utf8`/`undefined` 默认编码下均返回 `'abcde'`。调用 `buf.toJSON()` 或 `JSON.stringify(buf)` 会隐式输出 `{"type":"Buffer","data":[1,2,3,4,5]}`,反序列化时在 `JSON.parse` 的 reviver 中按 `value.type === 'Buffer'` 即可还原为等价 `Buffer` 实例。

缓冲区合并使用 `Buffer.concat(list[, totalLength])` 生成新 `Buffer`,传入 `totalLength` 时按目标字节数截断或补零(实测 `'蓝色天空'` 与 `'-Node'` 合并为 `'蓝色天空-Node'`,限定 12 字节截断为 `'蓝色天空'`)。缓冲区比较使用 `buf.compare(otherBuffer)`(Node.js v0.12.2 引入),按字节字典序返回负值、0 或正值(实测 `'ABC'` 与 `'ABCD'`/`'ABC'`/`'ABD'` 符号依次为 `[-1, 0, 1]`)。

拷贝使用 `buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])`:注意原文称无返回值,但在 Node.js 实际实现中 `copy()` 会返回实际拷贝的字节数(实测将 `'NODEJS'` 拷贝到 `'abcdefghijkl'` 索引 2 处返回 `6` 并得到 `'abNODEJSijkl'`)。裁剪切片 `buf.slice([start[, end]])`(现代推荐等价方法 `buf.subarray()`)与原 `Buffer` 共享同一底层内存,实测修改子视图首字节为 `'Z'` 后原缓冲同步变为 `'Zbcdef'`。`buf.length` 始终返回已分配的总字节数而非字符串字符数(实测 `'蓝色天空'` 字符串长度为 4、UTF-8 Buffer 字节长度为 12,256 字节缓冲区写入 14 字节后 `length` 仍为 256)。

二进制整数与浮点读写及方法参考手册

下表完整对应来源 49 项 Buffer 常用方法,并结合 Node.js v24.22.3 实测修正历史差异:所有 `read*`/`write*` 方法在 Node.js v10+ 已移除旧 `noAssert` 参数,越界或超范围写入一律严格抛出 `ERR_OUT_OF_RANGE`;同时实测 `writeUIntLE(0x1234567890ab, 0, 6)` 输出小端 `'ab9078563412'`、`writeUIntBE` 输出大端 `'1234567890ab'`,16/32 位有符号整数与 IEEE 754 32 位单精度浮点 `1.5`、64 位双精度浮点 `3.25` 及 `fill(0x7f, 1, 4)` 均读写往返精确无误。

Node.js Buffer 常用方法与二进制读写参考

方法与签名参数说明返回值与行为异常与版本说明
new Buffer(size)size: 字节数分配 size 字节缓冲已废弃(DEP0005),请改用 Buffer.alloc() 或 Buffer.allocUnsafe();超 kMaxLength 抛 RangeError
new Buffer(buffer)buffer: 源 Buffer拷贝源数据创建新实例已废弃,请改用 Buffer.from(buffer)
new Buffer(str[, encoding])str: 字符串;encoding: 默认 'utf8'按编码分配含字符串内容的缓冲已废弃,请改用 Buffer.from(string[, encoding])
buf.length无(只读属性)返回已分配内存的总字节数固定不变,不随写入有效字节数变化
buf.write(string[, offset[, length]][, encoding])string/offset(0)/length(余长)/encoding('utf8')返回实际写入字节数;不截断半个多字节字符offset 越界抛 ERR_OUT_OF_RANGE
buf.writeUIntLE(value, offset, byteLength)value: 无符号整数;offset;byteLength(1~6)按小端序写入最多 48 位无符号整数,返回 offset + byteLengthNode v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.writeUIntBE(value, offset, byteLength)value: 无符号整数;offset;byteLength(1~6)按大端序写入最多 48 位无符号整数,返回 offset + byteLengthNode v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.writeIntLE(value, offset, byteLength)value: 有符号整数;offset;byteLength(1~6)按小端序写入最多 48 位有符号整数,返回 offset + byteLengthNode v10+ 已移除 noAssert,超范围抛 ERR_OUT_OF_RANGE
buf.writeIntBE(value, offset, byteLength)value: 有符号整数;offset;byteLength(1~6)按大端序写入最多 48 位有符号整数,返回 offset + byteLengthNode v10+ 已移除 noAssert,超范围抛 ERR_OUT_OF_RANGE
buf.readUIntLE(offset, byteLength)offset: 起始偏移;byteLength(1~6)按小端序读取最多 48 位无符号整数Node v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.readUIntBE(offset, byteLength)offset: 起始偏移;byteLength(1~6)按大端序读取最多 48 位无符号整数Node v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.readIntLE(offset, byteLength)offset: 起始偏移;byteLength(1~6)按小端序读取最多 48 位有符号整数(补码)Node v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.readIntBE(offset, byteLength)offset: 起始偏移;byteLength(1~6)按大端序读取最多 48 位有符号整数(补码)Node v10+ 已移除 noAssert,越界抛 ERR_OUT_OF_RANGE
buf.toString([encoding[, start[, end]]])encoding('utf8');start(0);end(buf.length)将指定字节区间解码为字符串并返回未知编码抛出 TypeError
buf.toJSON()无返回 { type: 'Buffer', data: number[] } 纯对象JSON.stringify(buf) 会自动调用此方法
buf[index]index: 字节索引 0 ~ length-1读写单个 8 位无符号字节(0x00~0xFF / 0~255)写入超出 0~255 按低 8 位截断,越界索引忽略
buf.equals(otherBuffer)otherBuffer: 待比较 Buffer/Uint8Array字节内容完全相同时返回 true,否则返回 false非 Uint8Array 参数抛出 ERR_INVALID_ARG_TYPE
buf.compare(otherBuffer)otherBuffer: 目标 Buffer/Uint8Array按字典序返回 -1、0 或 1适合作为 Array.prototype.sort 比较器
buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])target 及可选目标/源起止偏移拷贝字节并返回实际复制的字节数;源和目标可重叠实测返回拷贝字节数而非 undefined
buf.slice([start[, end]])start(0)、end(length),支持负索引返回共享同一底层内存的新视图Node v16+ 建议改用 buf.subarray([start[, end]])
buf.readUInt8([offset])offset: 默认 0读取无符号 8 位整数(0~255)Node v10+ 始终执行边界检查,越界抛 ERR_OUT_OF_RANGE
buf.readUInt16LE([offset])offset: 默认 0按小端序读取无符号 16 位整数(0~65535)需至少剩余 2 字节,否则抛 ERR_OUT_OF_RANGE
buf.readUInt16BE([offset])offset: 默认 0按大端序读取无符号 16 位整数(0~65535)需至少剩余 2 字节,否则抛 ERR_OUT_OF_RANGE
buf.readUInt32LE([offset])offset: 默认 0按小端序读取无符号 32 位整数需至少剩余 4 字节,否则抛 ERR_OUT_OF_RANGE
buf.readUInt32BE([offset])offset: 默认 0按大端序读取无符号 32 位整数需至少剩余 4 字节,否则抛 ERR_OUT_OF_RANGE
buf.readInt8([offset])offset: 默认 0读取有符号 8 位整数(-128~127)越界抛 ERR_OUT_OF_RANGE
buf.readInt16LE([offset])offset: 默认 0按小端序读取有符号 16 位整数(-32768~32767)越界抛 ERR_OUT_OF_RANGE
buf.readInt16BE([offset])offset: 默认 0按大端序读取有符号 16 位整数(-32768~32767)越界抛 ERR_OUT_OF_RANGE
buf.readInt32LE([offset])offset: 默认 0按小端序读取有符号 32 位整数越界抛 ERR_OUT_OF_RANGE
buf.readInt32BE([offset])offset: 默认 0按大端序读取有符号 32 位整数越界抛 ERR_OUT_OF_RANGE
buf.readFloatLE([offset])offset: 默认 0按小端序读取 IEEE 754 32 位单精度浮点数纠正来源笔误:Float 为 32 位单精度而非双精度
buf.readFloatBE([offset])offset: 默认 0按大端序读取 IEEE 754 32 位单精度浮点数纠正来源笔误:Float 为 32 位单精度而非双精度
buf.readDoubleLE([offset])offset: 默认 0按小端序读取 IEEE 754 64 位双精度浮点数需至少剩余 8 字节,否则抛 ERR_OUT_OF_RANGE
buf.readDoubleBE([offset])offset: 默认 0按大端序读取 IEEE 754 64 位双精度浮点数需至少剩余 8 字节,否则抛 ERR_OUT_OF_RANGE
buf.writeUInt8(value[, offset])value(0~255);offset(0)写入无符号 8 位整数,返回 offset + 1超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeUInt16LE(value[, offset])value(0~65535);offset(0)按小端序写入无符号 16 位整数,返回 offset + 2超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeUInt16BE(value[, offset])value(0~65535);offset(0)按大端序写入无符号 16 位整数,返回 offset + 2超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeUInt32LE(value[, offset])value(0~0xffffffff);offset(0)按小端序写入无符号 32 位整数,返回 offset + 4超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeUInt32BE(value[, offset])value(0~0xffffffff);offset(0)按大端序写入无符号 32 位整数,返回 offset + 4纠正来源笔误:本方法接收无符号而非有符号 32 位整数
buf.writeInt8(value[, offset])value(-128~127);offset(0)写入有符号 8 位整数,返回 offset + 1补全来源缺失说明;超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeInt16LE(value[, offset])value(-32768~32767);offset(0)按小端序写入有符号 16 位整数,返回 offset + 2超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeInt16BE(value[, offset])value(-32768~32767);offset(0)按大端序写入有符号 16 位整数,返回 offset + 2超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeInt32LE(value[, offset])value(-2^31~2^31-1);offset(0)按小端序写入有符号 32 位整数,返回 offset + 4超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeInt32BE(value[, offset])value(-2^31~2^31-1);offset(0)按大端序写入有符号 32 位整数,返回 offset + 4超范围或越界抛 ERR_OUT_OF_RANGE
buf.writeFloatLE(value[, offset])value: 32 位浮点数;offset(0)按小端序写入 IEEE 754 32 位浮点数,返回 offset + 4越界抛 ERR_OUT_OF_RANGE
buf.writeFloatBE(value[, offset])value: 32 位浮点数;offset(0)按大端序写入 IEEE 754 32 位浮点数,返回 offset + 4越界抛 ERR_OUT_OF_RANGE
buf.writeDoubleLE(value[, offset])value: 64 位双精度浮点;offset(0)按小端序写入 IEEE 754 64 位浮点数,返回 offset + 8越界抛 ERR_OUT_OF_RANGE
buf.writeDoubleBE(value[, offset])value: 64 位双精度浮点;offset(0)按大端序写入 IEEE 754 64 位浮点数,返回 offset + 8越界抛 ERR_OUT_OF_RANGE
buf.fill(value[, offset[, end]][, encoding])value: 字节/字符串/Buffer;offset(0);end(length)用指定值重复填充目标区间并返回当前 Buffer 实例配合 allocUnsafe() 清零或初始化最常用

实测 Buffer 安全创建、字符编码转换、读写/JSON/合并/比较/拷贝/切片与大小端二进制读写

含完整源码 · 191 行
展开查看收起
javascript
function deepEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}

// Case 1: Buffer.alloc / allocUnsafe / allocUnsafeSlow / Buffer.from & character encodings
const allocZero = Buffer.alloc(4);
const allocFilled = Buffer.alloc(4, 1);
const allocUnsafeFilled = Buffer.allocUnsafe(4).fill(0x2a);
const allocSlowFilled = Buffer.allocUnsafeSlow(4).fill(0x05);
const fromArray = Buffer.from([1, 2, 3]);
const ab = new ArrayBuffer(4);
const viewFromAb = Buffer.from(ab);
viewFromAb[0] = 0x41;
const sharedFirstByte = new Uint8Array(ab)[0];
const copiedBuf = Buffer.from(fromArray);
copiedBuf[0] = 99;

const asciiBuf = Buffer.from('node24', 'ascii');
const utf8Accent = Array.from(Buffer.from('tést', 'utf8'));
const latin1Accent = Array.from(Buffer.from('tést', 'latin1'));
const utf16Hex = Buffer.from('AB', 'utf16le').toString('hex');

const actual1 = {
  allocZero: Array.from(allocZero),
  allocFilled: Array.from(allocFilled),
  allocUnsafeFilled: Array.from(allocUnsafeFilled),
  allocSlowFilled: Array.from(allocSlowFilled),
  fromArrayUnchanged: Array.from(fromArray),
  sharedFirstByte,
  hex: asciiBuf.toString('hex'),
  base64: asciiBuf.toString('base64'),
  utf8Accent,
  latin1Accent,
  utf16Hex,
};
const expected1 = {
  allocZero: [0, 0, 0, 0],
  allocFilled: [1, 1, 1, 1],
  allocUnsafeFilled: [42, 42, 42, 42],
  allocSlowFilled: [5, 5, 5, 5],
  fromArrayUnchanged: [1, 2, 3],
  sharedFirstByte: 65,
  hex: '6e6f64653234',
  base64: 'bm9kZTI0',
  utf8Accent: [0x74, 0xc3, 0xa9, 0x73, 0x74],
  latin1Accent: [0x74, 0xe9, 0x73, 0x74],
  utf16Hex: '41004200',
};

// Case 2: buf.write, buf.length, buf.toString, buf.toJSON & JSON roundtrip
const wBuf = Buffer.alloc(256);
const writtenBytes = wBuf.write('mbsky-node-buf');
const alphaBuf = Buffer.alloc(26);
for (let i = 0; i < 26; i += 1) {
  alphaBuf[i] = i + 97;
}
const jsonSrc = Buffer.from([1, 2, 3, 4, 5]);
const jsonStr = JSON.stringify(jsonSrc);
const restored = JSON.parse(jsonStr, (_, value) =>
  value && value.type === 'Buffer' ? Buffer.from(value.data) : value,
);

const actual2 = {
  allocLength: wBuf.length,
  writtenBytes,
  writtenSlice: wBuf.toString('utf8', 0, writtenBytes),
  alphaFull: alphaBuf.toString('ascii'),
  alphaAscii5: alphaBuf.toString('ascii', 0, 5),
  alphaUtf85: alphaBuf.toString('utf8', 0, 5),
  alphaDefault5: alphaBuf.toString(undefined, 0, 5),
  jsonStr,
  restoredIsBuffer: Buffer.isBuffer(restored),
  restoredEquals: restored.equals(jsonSrc),
};
const expected2 = {
  allocLength: 256,
  writtenBytes: 14,
  writtenSlice: 'mbsky-node-buf',
  alphaFull: 'abcdefghijklmnopqrstuvwxyz',
  alphaAscii5: 'abcde',
  alphaUtf85: 'abcde',
  alphaDefault5: 'abcde',
  jsonStr: '{"type":"Buffer","data":[1,2,3,4,5]}',
  restoredIsBuffer: true,
  restoredEquals: true,
};

// Case 3: Buffer.concat, compare, equals, copy, subarray/slice shared memory
const c1 = Buffer.from('蓝色天空');
const c2 = Buffer.from('-Node');
const merged = Buffer.concat([c1, c2]);
const mergedTruncated = Buffer.concat([c1, c2], 12);

const cmpBefore = Buffer.from('ABC').compare(Buffer.from('ABCD'));
const cmpEqual = Buffer.from('ABC').compare(Buffer.from('ABC'));
const cmpAfter = Buffer.from('ABD').compare(Buffer.from('ABC'));

const target = Buffer.from('abcdefghijkl');
const insert = Buffer.from('NODEJS');
const bytesCopied = insert.copy(target, 2);

const orig = Buffer.from('abcdef');
const sub = orig.subarray(0, 2);
sub[0] = 0x5a; // 'Z'

const actual3 = {
  mergedText: merged.toString('utf8'),
  mergedTruncatedText: mergedTruncated.toString('utf8'),
  cmpSigns: [Math.sign(cmpBefore), Math.sign(cmpEqual), Math.sign(cmpAfter)],
  bytesCopied,
  afterCopy: target.toString('utf8'),
  subText: sub.toString('utf8'),
  origMutatedBySub: orig.toString('utf8'),
};
const expected3 = {
  mergedText: '蓝色天空-Node',
  mergedTruncatedText: '蓝色天空',
  cmpSigns: [-1, 0, 1],
  bytesCopied: 6,
  afterCopy: 'abNODEJSijkl',
  subText: 'Zb',
  origMutatedBySub: 'Zbcdef',
};

// Case 4: Binary endian integer & float read/write + fill & index access
const le6 = Buffer.alloc(6);
le6.writeUIntLE(0x1234567890ab, 0, 6);
const be6 = Buffer.alloc(6);
be6.writeUIntBE(0x1234567890ab, 0, 6);

const numBuf = Buffer.alloc(18);
numBuf.writeInt16LE(-1234, 0);
numBuf.writeInt32BE(0x01020304, 2);
numBuf.writeFloatLE(1.5, 6);
numBuf.writeDoubleBE(3.25, 10);

const fillBuf = Buffer.alloc(5, 0);
fillBuf.fill(0x7f, 1, 4);

const actual4 = {
  le6Hex: le6.toString('hex'),
  be6Hex: be6.toString('hex'),
  readUIntLE: le6.readUIntLE(0, 6),
  readUIntBE: be6.readUIntBE(0, 6),
  readInt16LE: numBuf.readInt16LE(0),
  readInt32BE: numBuf.readInt32BE(2),
  readFloatLE: numBuf.readFloatLE(6),
  readDoubleBE: numBuf.readDoubleBE(10),
  fillBytes: Array.from(fillBuf),
};
const expected4 = {
  le6Hex: 'ab9078563412',
  be6Hex: '1234567890ab',
  readUIntLE: 0x1234567890ab,
  readUIntBE: 0x1234567890ab,
  readInt16LE: -1234,
  readInt32BE: 0x01020304,
  readFloatLE: 1.5,
  readDoubleBE: 3.25,
  fillBytes: [0, 127, 127, 127, 0],
};

// Case 5: Multi-byte UTF-8 byte length vs string length & partial write safety
const zhStr = '蓝色天空';
const zhBuf = Buffer.from(zhStr, 'utf8');
const smallBuf = Buffer.alloc(5); // can only hold 1 Chinese char (3 bytes), refuses partial 2nd char
const partialWritten = smallBuf.write(zhStr, 0, 'utf8');

const actual5 = {
  stringLength: zhStr.length,
  bufferByteLength: zhBuf.length,
  partialWritten,
  partialDecoded: smallBuf.toString('utf8', 0, partialWritten),
};
const expected5 = {
  stringLength: 4,
  bufferByteLength: 12,
  partialWritten: 3,
  partialDecoded: '蓝',
};

const cases = [
  { name: 'alloc_from_and_encodings', passed: deepEqual(actual1, expected1), expected: expected1, actual: actual1 },
  { name: 'write_tostring_length_and_json', passed: deepEqual(actual2, expected2), expected: expected2, actual: actual2 },
  { name: 'concat_compare_equals_copy_slice', passed: deepEqual(actual3, expected3), expected: expected3, actual: actual3 },
  { name: 'endian_int_and_float_rw', passed: deepEqual(actual4, expected4), expected: expected4, actual: actual4 },
  { name: 'utf8_byte_length_and_safe_write', passed: deepEqual(actual5, expected5), expected: expected5, actual: actual5 },
];

console.log(JSON.stringify({ version: process.version, cases }));
核对 UTF-8 与 Latin-1 字节差异、write 安全截断中文、JSON 往返还原、subarray 共享内存及 48 位大小端与浮点读写。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a1041.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Node.js

课程关联工程包(登记关联38章) · 含当前章节或实例 · mbskycom202612a1041.zip

下载课程关联工程包:Node.js

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1042.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

`Buffer.alloc(10)` 会将分配的 10 字节初始化为 0,`Buffer.allocUnsafe(10)` 跳过清零以换取性能但可能暴露旧内存数据,必须先覆盖再使用;`subarray()`(旧 `slice()`)仅创建指向同一底层内存区间的视图,未发生拷贝。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习