blob: 7b0761cb312d8b1086d82245d21deb5e295b0488 [file] [log] [blame]
const assert = require('assert');
function padding(str, padding) {
const offset = padding - str.length;
return '0'.repeat(offset) + str;
}
function binaryNum1(num, byteLength) {
const str = num.toString(2);
//补齐位数
const paddingStr = padding(str, byteLength * 8);
const buffer = Buffer.alloc(byteLength);
for (let i = 0; i < byteLength; i++) {
const start = i * 8;
const end = start + 8;
buffer[i] = parseInt(paddingStr.substring(start, end), 2);
}
return buffer;
}
// 在源文件中进行了搜索,发现 binaryNum 的作用就是对 big-endian 的 uint32 和 uint64 进行序列化
// 所以该实现只针对这样两种类型
function binaryNum2(num, byteLength) {
const buf = Buffer.allocUnsafe(byteLength);
if (num <= Number.MAX_SAFE_INTEGER) {
if (byteLength === 4) {
buf.writeUInt32BE(num, 0);
} else {
buf.writeUInt32BE(0, 0);
buf.writeUInt32BE(num, 4);
}
} else {
const high = Math.floor(num / Math.pow(2, 32));
const low = num & 0xffffffff;
buf.writeUInt32BE(high);
buf.writeUInt32BE(low, 4);
}
return buf;
}
// test
assert.ok(
binaryNum1(1025, 4).equals(Buffer.from([0x00, 0x00, 0x04, 0x01])),
'1 - 1025',
);
assert.ok(
binaryNum1(201212, 8).equals(
Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x11, 0xfc]),
),
'1 - 201212',
);
assert.ok(
binaryNum1(13234234234234234234, 8).equals(
Buffer.from([0xb7, 0xa9, 0x71, 0xeb, 0x05, 0xd8, 0x68, 0x00]),
),
'1 - 13234234234234234234',
);
assert.ok(
binaryNum2(1025, 4).equals(Buffer.from([0x00, 0x00, 0x04, 0x01])),
'2 - 1025',
);
assert.ok(
binaryNum2(201212, 8).equals(
Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x11, 0xfc]),
),
'2 - 201212',
);
assert.ok(
binaryNum2(13234234234234234234, 8).equals(
Buffer.from([0xb7, 0xa9, 0x71, 0xeb, 0x05, 0xd8, 0x68, 0x00]),
),
'2 - 13234234234234234234',
);
// benchmark
console.time('1 - 1025');
for (let i = 0; i < 1000000; i++) {
binaryNum1(1025, 4);
}
console.timeEnd('1 - 1025');
console.time('2 - 1025');
for (let i = 0; i < 1000000; i++) {
binaryNum2(1025, 4);
}
console.timeEnd('2 - 1025');
console.time('1 - 201212');
for (let i = 0; i < 1000000; i++) {
binaryNum1(201212, 4);
}
console.timeEnd('1 - 201212');
console.time('2 - 201212');
for (let i = 0; i < 1000000; i++) {
binaryNum2(201212, 4);
}
console.timeEnd('2 - 201212');
console.time('1 - 13234234234234234234');
for (let i = 0; i < 1000000; i++) {
binaryNum1(13234234234234234234, 8);
}
console.timeEnd('1 - 13234234234234234234');
console.time('2 - 13234234234234234234');
for (let i = 0; i < 1000000; i++) {
binaryNum2(13234234234234234234, 8);
}
console.timeEnd('2 - 13234234234234234234');