kriszyp / msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add an option to decode any integer types as bigint

yy0931 opened this issue · comments

For example, if you encode an int into a messagepack in Python, It is encoded into different int types depending on its value.

import msgpack  # pip install msgpack
print([*msgpack.packb(12345)])  # uint16: [205, 48, 57]
print([*msgpack.packb(123456789012345678)])  # uint64: [207, 1, 182, 155, 75, 166, 48, 243, 78]

However, msgpackr decodes only int64 and uint64 as bigint, and the others as number.

const msgpackr = require("msgpackr")
msgpackr.unpack(new Uint8Array([205, 48, 57]))  // 12345
msgpackr.unpack(new Uint8Array([207, 1, 182, 155, 75, 166, 48, 243, 78]))  // 123456789012345678n

Therefore, all values that can be bigints need to be explicitly converted to bigint (e.g. BigInt(x) + BigInt(y)) to prevent the Cannot mix BigInt and other types error.
It would be nice to have an option to convert all integer types to bigint.

There is not any native JS support for converting directly from 8, 16, or 32-bit binary data directly to BigInt, so msgpackr would not have (performance) advantage to outputting BigInts, it would just be doing calls like getUint32() and then calling BigInt(value), which is the same thing you are doing externally. In fact it would probably be a bit slower for msgpackr to do this, because it would need to consult a flag on each number to know whether to convert BigInt or leave as a number, so this is probably best done externally/outside the library.

Also, it is worth noting that if what you are looking for is consistency in number types, you can alternately use int64AsNumber to ensure that 64-bit numbers are also output as numbers (not BigInt), which has integer precision up to about 2^56.

Thank you for the response. I understand it is certainly not a good idea to degrade performance for minor use cases for a library claiming to be the fastest.