Had to dig through quite a few articles to get a simple answer to this one, so I figured I'd put it right at the beginning. Yes, regular math calculations using
BigInt cost more for the computer to execute than if you were just using Numbers.
For a little more detail on that, please read on.
Ol' fashioned integers. 0-10. Your grandad's math. Numbers are....
- Primitive (meaning they cannot be changed no matter what)
- 64-bit (meaning there is a size limit)
- Is always a float (meaning
17.0 === 17returns
And just for fun, here are some examples of numbers.
There are those who think it's weird that
NaN, standing for Not-A-Number, is of type
Number -- but honestly, it provides more detail about itself as an error if it's still associated with
Large, hefty numbers. Could eat your grandad's numbers for breakfast. Here are some
- Also Primitive
Does not like to mix with numbers (
10 * BigInt(10)will result in a type error)
Does somewhat mix with
- Puts a fancy little
nat the end of the number
It's helpful to play around with
- note that the
BigIntmethod here will accept a string
- note that the
Your intuition probably led you to this already, but there is the one obvious thing:
BigInts are bigger. Not in the way that 100 is bigger than 10, but in the way that your computer receives and handles the
BigInt data type. The memory set aside for a
BigInt is always larger than a regular
Number, regardless of the value of the
BigInt itself. So, if you have two variables:
let optionA = 15
let optionB = 15n
Both are 15, but optionB is clunkier. Thicker.
A side note on equality:
Numbers, you should use loose equality (
The other reason
10*81, your computer uses its hardware, which is easily accessible and requires almost no searching or computational power. When you try something special like
10n * 81n, it has to go and find the
Run to see it in action.
The only reason I went down this rabbit hole was for interview prep: practicing algorithms. There is an interesting LeetCode problem (found here) which asks you to add two linked lists together and return their sum as a linked list. After solving the initial prompt, my code was still failing because some of the test cases resulted in massive....some might say
To resolve this problem, my first instinct was, "Well, what if we treat every single number like it's gonna be a
BigInt?" And the answer to that question is that the solution will pass, but it will be slower than a...car that's slow...or...whatever, it'll make your algorithm super slow. So you really need to allow for and handle
BigInts separately, giving them the attention they deserve as unique circumstances.
Interview prep is a huge practical reality for software engineers of all levels, but there are also real-world applications for
BigInts, such as massive database management, precise math with large numbers for financial applications, high-accuracy timestamps, and anything else people do with fancy and elaborate math. Who knows, maybe rocket science.
There is a lot more to the story with the
BigInt datatype, but hopefully this gave a useful and concise introduction. Have fun & feel free to drop a comment with any additions, questions, or thoughts!