fast-bigint
Module for fast operations with long integers.
Arithmetic operations are written in C.
Usage
const FastBigInt = ; // Create using numberconst a = 123; // Create using little-endian bufferconst b = Buffer; // Add using 'add(a, b)'const x = FastBigInt;// Subtract using 'sub(a, b)'const x = FastBigInt;// Multiply using 'mul(a, b)'const x = FastBigInt;// Divide using 'div(a, b)'const x = FastBigInt;// Modulo using 'mod(a, b)'const x = FastBigInt; // Set thread limit for async functions (default 8)FastBigInt; // Run async on new threadconst x = await FastBigInt;const x = await FastBigInt;const x = await FastBigInt;const x = await FastBigInt;const x = await FastBigInt; FastBigInt;FastBigInt;FastBigInt;FastBigInt;FastBigInt; //Get little-endian buffer//Buffer size is padded to multiple of 8const x = 8;x // = <Buffer 08 00 00 00 00 00 00 00> //Compare using compare(a, b)//returns// -1 if a < b// 0 if a = b// 1 if a > bFastBigInt; // = -1 //Compare using compare functions5; // = true
Warning
This module only supports positive numbers, so sub(a, b)
when a < b
will return invalid values
Performance
It's currently faster than JavaScript BigInt
- When adding or subtracting numbers larger than ~98304 bytes
- When multiplying numbers larger than ~12288 bytes
Division and modulo are now slower than JS BigInt, I will try to improve the algorithm in the future
You can perform calculations on multiple threads with async functions (unlike JavaScript, which is single-threaded)