DEV Community

Discussion on: Are BigInt Math Calculations More Expensive Than Using Numbers in JavaScript?

jamesliudotcc profile image
James Liu


Let's make this mental leap: They were keeping the numbers as linked lists because they overflowed safe-ints.

What would be better, maintaining a linked list math algorithms library inside your engineering team, or using bigints and having the V8 team maintain the bigint functionality for you?

Also, even if your algorithm is basically, try using number arithmetic, check if the result is of type int, if not, try again using BigInt, is that worthwhile keeping in your code instead of keeping BigInts natively? What is the cost of converting the numbers back and forth between the linked list and the number types?

Also, how are we persisting these numbers to disk? I don't think databases generally support integers larger than 64 bits.

And, supposing you decided to switch. How do you weigh the pros and cons of refactoring to bigints?

halented profile image
Hal Dunn Author

Interesting thoughts, thanks for the comment! As for your first question, whether keeping a linked-list library would be better than using V8 — it would be tough to gauge, as I think it depends on how frequently your library is used, how efficiently the methods run, and the intent behind creating the library in the first place: will it be purely internal? Will the company publish it as open source down the line? There are a lot of unknowns there. Instinctively, I would lean towards using V8 because they have had years to test and improve on software which solves specifically this type of problem.
With that said, I think the cost of conversion from lists to numbers (to possibly BigInts) and back to lists is pretty high, especially if we assume this process would happen thousands of times a day.
If tasked with persisting the numbers to the disk, I would store them as Strings, one and all, the way JSON does — that way we don’t need to deal with whether or not they are BigInts unless they are being used in the functional side of the app.
Overall, your questions really bring to light the fact that the problem as posed to me didn’t contain enough detail to tell whether using BigInts could be considered “worth it” or not. I think it would be a situational answer, which would need hard data like type & frequency of requests to really make a call.