Library for working with large numbers and fractions using BigInt.
It provides advanced functionality for performing arithmetic operations with precision and decimal handling.
Documentation.
- 100% test coverage.
- No dependencies.
- No limitations on the size of the numbers, except for the limitations of
BigInt, so limited by system memory =) - Fully written in TypeScript, so it provides type definitions out of the box.
- Tree-shaking is supported.
Install big.esm using Bun:
bun add big.esmor npm:
npm install big.esmor yarn:
yarn add big.esmimport { addBig, cloneBig, createBig } from "big.esm";
const a = createBig("12345678910.12345678910");
const b = createBig("9876543210.9876543210");
addBig(a, b);
console.log(a.toString()); // 22222222121.1111111101
// If you need immutability, clone first:
const c = cloneBig(a);
addBig(c, b);Pipeline (chainable, mutates its internal Big):
import { pipeBig } from "big.esm";
console.log(
pipeBig("1.5e3").add(500).div(2, 0, "down").toString()
); // 1000More info - BENCHMARK.md
- Speed (ops):
big.esm v2vsbig.js—26.26x faster(median; min2.17x, max154.43x) →2.26x fasterthanbig.esm v1(mutable mode; median11.63xvsbig.js). - Speed (pipeBig):
pipeBigvsbig.js—29.8x faster(median; min2.84x, max148.27x). - Size (full):
big.esm v2—5.25 KBminified /1.87 KBbrotli (≈1.30x/1.46xsmaller thanbig.js). - Size (pipe-only):
bigPipe (v2)—4.40 KBminified /1.62 KBbrotli (≈1.55x/1.68xsmaller thanbig.js).
big.esm is compatible with all modern browsers, Bun 1.0+, and Node.js 16+. It uses BigInt internally, so it is not compatible with older runtimes. On info from caniuse.com BigInt is supported by 98.84% of all browsers(as of 2025-12-18).
big.esmis not a drop-in replacement forbig.js. It does not support the same API and does not have the same functionality. It is a completely different library.
Starting from v2, the library is mutable-first to stay small and fast (fewer allocations, less GC, no runtime "clone vs mutate" checks in hot paths):
- Arithmetic helpers (
addBig,subBig,mulBig,divBig,modBig,powBig,sqrtBig,absBig) mutate the first argument and return it. alignScale(a, b)mutates the operand with the smaller scale.minBig/maxBigreturn one of the inputs (no cloning).pipeBig(big)/new BigPipe(big)new api, mutates the passedBig.
If these changes are breaking for your codebase, clone before calling operations (cloneBig(a)) or adjust your code to work with mutation.
More information in the documentation.
Creates a Big instance from a string, number, bigint, Big, or BigObject. Preferred constructor helper.
The scale is the number of digits to the right of the decimal point. If the scale is not specified, it will be calculated automatically, otherwise the number will be rounded to integer.
Creates a new Big instance from another Big instance.
Aligns the scale of two Big instances by mutating the one with the smaller scale. The scale of the result will be equal to the maximum scale of the two numbers
Checks if the value is a valid numeric value for creating a Big instance.
Math operations mutate the first argument and return it.
Adds b to a (mutates a).
Subtracts b from a (mutates a).
Multiplies a by b (mutates a).
Divides a by b (mutates a).
Calculates a % b (mutates a).
Raises a to exp (mutates a).
Calculates the root of a (mutates a).
Replaces a with |a| (mutates a).
Compares two Big instances. Returns -1 if a < b, 0 if a == b and 1 if a > b.
Returns the minimum of a and b by reference (no cloning).
Returns the maximum of a and b by reference (no cloning).
Creates a chainable pipeline that mutates its internal Big in-place.
Class form of the pipeline.
- Generate documentation from JSDoc
- Remove trailing zeros from toString method
- Add formatting options
- Add more mathematical functions
MIT
© 2023 dschewchenko