Typescript BigInt Vs Number

In this article, we will show you the difference between BigInt and number data types. And also when to use the BigInt data type

BigInt Vs Number

BigInt is an integer, number is a decimal

You cannot store 100.20 in a BigInt, because it is an integer and not decimal.

They are implemented differently

bigInt is stored as arbitrary-precision integers and the number as double-precision 64-bit number.

BigInt can handle large numbers

The number can handle numbers up to 9007199254740991 ( Number.MAX_SAFE_INTEGER). It is a limitation imposed due to the double precison 64 bit number

The BigInt can handle numbers larger than that. BigInt is limited by the available memory of the host system.

BigInt cannot be mixed with numbers

bigint can not be mixed with operations where numbers are involved as shown below.

Coercing BigInt to number may lose precision

bigInt are converted to a number by using the Number function

You can compare bigInt with a number

You can compare a bigInt with a number.

BigInt is slow

The arithmetic operations involving BigInt are slower compared to the Floating Point arithmetic of a primitive number.

The floating-point arithmetic is implemented in hardware, Hence it is faster. The Arbitrary-precision arithmetic is entirely implemented in software hence slower.

When to use BigInt

  1. The bigInt is the only option if you need to support numbers larger than Number.MAX_SAFE_INTEGER
  2. The precision of calculations are more important than the speed

Summary

In this article, we learned the difference between BigInt and number data type in Typescript.

3 thoughts on “Typescript BigInt Vs Number”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top