miktam / sizeof

Get size of a JavaScript object
MIT License
301 stars 45 forks source link

[BUG] BigInt in object #88

Closed wanghm25 closed 1 year ago

wanghm25 commented 1 year ago

I found a problem with code like this (typescript):

import sizeof from "object-sizeof";

const obj = {
  num: BigInt(123123123123123123),
}

console.log(sizeof(obj));

And get error:

Error detected, return -1 TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at objectSizeComplex (/Users/bytedance/node/node_modules/object-sizeof/indexv2.js:62:33)
...

The reason is, there is a JSON.stringify in objectSizeComplex: const objectToString = JSON.stringify(potentialConversion)

I think,there should be a recursive call in objectSizeComplex

lukahukur commented 1 year ago

Ye, same issue

lukahukur commented 1 year ago
   JSON.stringify(
        Object,
        (key, value) => (typeof value === 'bigint' ? value.toString() : value) // return everything else unchanged
      )

just add this