Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

V8 BigInt size in memory?

Tags:

node.js

v8

bigint

Is there a way to get the occupied memory size in bytes of BigInt numbers?

let a = BigInt(99999n)
console.log(a.length) // yield undefined

Thanks

like image 369
Larry Avatar asked Oct 25 '25 15:10

Larry


1 Answers

V8 developer here. There is generally no way to determine the occupied memory size of an object, and BigInts are no exception. Why do you want to access it?

As far as the internal implementation in V8 is concerned, a BigInt has a small object header (currently two pointer sizes; this might change over time), and then a bit for every bit of the BigInt, rounded up to multiples of a pointer size. 99999 is a 17-bit number, so in your example let a = 99999n ("BigInt(99999n)" is superfluous!), the allocated BigInt will consume (2 + Math.ceil(17/64)) * 64 bits === 24 bytes on a 64-bit system.

It may or may not make sense to add length-related properties or methods (.bitLength?) to BigInts in the future. If you have a use case, I suggest you file an issue at https://github.com/tc39/proposal-bigint/issues so that it can be discussed.

like image 142
jmrk Avatar answered Oct 28 '25 07:10

jmrk