Closed florianbepunkt closed 1 year ago
I remain reluctant to replace the _isBigNumber === true
test with your suggested typeof triplet on probably the hottest code path in the library (regardless of whether or not it would seem to make any significant difference in performance).
Deserialization after structured cloning is pretty straightforward as it is:
const serialize = bn => JSON.stringify(structuredClone(bn));
const deserialize = json => new BigNumber({ _isBigNumber: true, ...JSON.parse(json) });
const a = new BigNumber('1.23');
const b = deserialize(serialize(a));
console.log(a.eq(b)); // true
I apologise for the late response, and thank you for your efforts.
On second thoughts, maybe I'll add a BigNumber.fromObject
method as suggested by #245. Anyway, I'll leave this open for now...
Fixes Rehydration after structural cloning #317.
An obj
{ s: 1, e: -2, c: [ 50 ] }
is not a BigNumber, henceBigNumber.isBigNumber(obj)
is false, but you can construct a BigNumber from it:new BigNumber(obj)
.This simplifies serialization/deserialization, since BigNumber values can be sent over the wire/inter process communication.