miktam / sizeof

Get size of a JavaScript object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] BigInt in object

wanghm25 opened this issue · comments

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

Ye, same issue

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

just add this