2pack 中文文档教程

发布于 5年前 浏览 27 项目主页 更新于 3年前

Codacy Badge

npm -g install 2pack
git clone https://github.com/Daeren/2pack.git
  • Binary (Little/Big - Endian)
  • Relative and absolute zero-copy operations wherever possible
  • Browser

Goals:

  1. Low memory usage;
  2. Maximum performance;
  3. Flexibility;
  4. Games.
const packer = require("2pack");

const header = packer("int16");
const payload = packer(["name:str", "hp:int16"]/*, unpackNewHolder, unpackDataAsArray*/);

//------------------------------

payload.offset = header.maxSize;

//---------]>

const id = 69;
const data = {name: "D", hp: 13};

const buf = header.pack(id, payload.pack(data));

//---------]>

console.log(
    packer.isBE,
    packer.isLE
);

console.log(
    header.unpack(buf, 0, buf.length),
    payload.unpack(buf, 0, buf.length)
);

//------------------------------

/*
packer("int16"); // primitive | returns: value
pack(0);

packer(["int16"]); // array | returns: array | unpackDataAsArray = true
pack([0]);

packer(["name:str"]); // object | returns: object
pack(["D"]);
pack({name: "D"});

packer(["int16", "name:str"]); // error

//----)>

unpack(bin, offset, length, cbEndInfo(offset), target, asCopy, asArray);

@bin - Buffer / Uint8Array
*/
> Node.js v8.9.4
> npm run bmCopm


2pack.pack: 2799.724ms
2pack.pack.static: 2251.945ms
2pack.unpack: 5212.609ms

msgpackLite.pack: 9648.407ms
msgpackLite.pack.static: 8721.737ms
msgpackLite.unpack: 12450.920ms

2pack.pack.without(str): 604.289ms
2pack.unpack.without(str): 1542.349ms

msgpackLite.pack.without(str): 5027.350ms
msgpackLite.unpack.without(str): 5191.954ms
Packet type
NameAliasNote
-
binbdefault: max 1024 (0-65535); server: Buffer; client: Uint8Array;
strsdefault: max 256 (0-65535)
intisize: 8, 16, 32, 64 (BigInt64Array)
uintusize: 8, 16, 32, 64 (BigUint64Array)
floatfsize: 32, 64
jsonjdefault: max 8192 (0-65535)

License

麻省理工学院


@Daeren @电报

Codacy Badge

npm -g install 2pack
git clone https://github.com/Daeren/2pack.git
  • Binary (Little/Big - Endian)
  • Relative and absolute zero-copy operations wherever possible
  • Browser

Goals:

  1. Low memory usage;
  2. Maximum performance;
  3. Flexibility;
  4. Games.
const packer = require("2pack");

const header = packer("int16");
const payload = packer(["name:str", "hp:int16"]/*, unpackNewHolder, unpackDataAsArray*/);

//------------------------------

payload.offset = header.maxSize;

//---------]>

const id = 69;
const data = {name: "D", hp: 13};

const buf = header.pack(id, payload.pack(data));

//---------]>

console.log(
    packer.isBE,
    packer.isLE
);

console.log(
    header.unpack(buf, 0, buf.length),
    payload.unpack(buf, 0, buf.length)
);

//------------------------------

/*
packer("int16"); // primitive | returns: value
pack(0);

packer(["int16"]); // array | returns: array | unpackDataAsArray = true
pack([0]);

packer(["name:str"]); // object | returns: object
pack(["D"]);
pack({name: "D"});

packer(["int16", "name:str"]); // error

//----)>

unpack(bin, offset, length, cbEndInfo(offset), target, asCopy, asArray);

@bin - Buffer / Uint8Array
*/
> Node.js v8.9.4
> npm run bmCopm


2pack.pack: 2799.724ms
2pack.pack.static: 2251.945ms
2pack.unpack: 5212.609ms

msgpackLite.pack: 9648.407ms
msgpackLite.pack.static: 8721.737ms
msgpackLite.unpack: 12450.920ms

2pack.pack.without(str): 604.289ms
2pack.unpack.without(str): 1542.349ms

msgpackLite.pack.without(str): 5027.350ms
msgpackLite.unpack.without(str): 5191.954ms
Packet type
NameAliasNote
-
binbdefault: max 1024 (0-65535); server: Buffer; client: Uint8Array;
strsdefault: max 256 (0-65535)
intisize: 8, 16, 32, 64 (BigInt64Array)
uintusize: 8, 16, 32, 64 (BigUint64Array)
floatfsize: 32, 64
jsonjdefault: max 8192 (0-65535)

License

MIT


@ Daeren @ Telegram

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文