JSBI

JSBI is a pure-JavaScript implementation of the official ECMAScript BigInt ...

README

JSBI — pure-JavaScript BigInts Build status jsbi on npm


JSBI is a pure-JavaScript implementation of the ECMAScript BigInt proposal, which officially became a part of the JavaScript language in ES2020.

Installation


  1. ```sh
  2. npm install jsbi --save
  3. ```

Usage


  1. ``` js
  2. import JSBI from './jsbi.mjs';

  3. const max = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
  4. console.log(String(max));
  5. // → '9007199254740991'
  6. const other = JSBI.BigInt('2');
  7. const result = JSBI.add(max, other);
  8. console.log(String(result));
  9. // → '9007199254740993'
  10. ```

Note: explicitly call toString on any JSBI instances when console.log()ing them to see their numeric representation (e.g. String(max) or max.toString()). Without it (e.g. console.log(max)), you’ll instead see the object that represents the value.

Use babel-plugin-transform-jsbi-to-bigint to transpile JSBI code into native BigInt code.

Refer to the detailed instructions below for more information.

Why?


Native BigInts are already shipping in modern browsers (at the time of this writing, Google Chrome 67+, Opera 54+, Firefox 68+, Edge 79+, Safari 14+) and Node.js (v10.4+), but some users are still running older browsers — which means you can't use them yet if you want your code to run everywhere.

To use BigInts in code that you want to run everywhere, you need a library. But there’s a difficulty: the BigInt proposal changes the behavior of operators (like +, >=, etc.) to work on BigInts. These changes are impossible to polyfill directly; and they are also making it infeasible (in most cases) to transpile BigInt code to fallback code using Babel or similar tools. The reason is that such a transpilation would have to replace every single operator in the program with a call to some function that performs type checks on its inputs, which would incur an unacceptable performance penalty.

The solution is to do it the other way round: write code using a library’s syntax, and transpile it to native BigInt code when available. JSBI is designed for exactly this purpose: it provides a BigInt “polyfill” implementation that behaves exactly like the upcoming native BigInts, but with a syntax that you can ship on all browsers, today.

Its advantages over other, existing big-integer libraries are:

- it behaves exactly like native BigInts do where they are available, so to eventually migrate to those, you can mechanically update your code’s syntax; no re-thinking of its logic will be required.
- strong focus on performance. On average, JSBI is performance-competitive with the native implementation that Google Chrome is currently shipping. (Note: we expect this statement to gradually become outdated as browsers invest in additional optimizations.)

How?


Except for mechanical differences in syntax, you use JSBI-BigInts just like you would use native BigInts. Some things even look the same, after you replaceBigInt with JSBI.BigInt:

OperationnativeJSBI
-------------------------------------------------------------------
Creation`a`a
Creation`a`a
Conversion`a.toString(radix)``a.toString(radix)`
Conversion`Number(a)``JSBI.toNumber(a)`
Truncation`BigInt.asIntN(64,`JSBI.asIntN(64,
|`JSBI.asUintN(64,
Type`typeof`a

Most operators are replaced by static functions:

OperationnativeJSBI
--------------------------------------------------------------------------
Addition`c`c
Subtraction`c`c
Multiplication`c`c
Division`c`c
Remainder`c`c
Exponentiation`c`c
Negation`b`b
Bitwise`b`b
Left`c`c
Right`c`c
Bitwise`c`c
Bitwise`cb`
Bitwise`c`c
Comparison`a`JSBI.equal(a,
|`JSBI.notEqual(a,
|`JSBI.lessThan(a,
|`JSBI.lessThanOrEqual(a,
|`JSBI.greaterThan(a,
|`JSBI.greaterThanOrEqual(a,

The functions above operate only on BigInts. (They don’t perform type checks in the current implementation, because such checks are a waste of time when we assume that you know what you’re doing. Don’t try to call them with other inputs, or you’ll get “interesting” failures!)

Some operations are particularly interesting when you give them inputs of mixed types, e.g. comparing a BigInt to a Number, or concatenating a string with a BigInt. They are implemented as static functions named after the respective native operators:

OperationnativeJSBI
-------------------------------------------------------------
Abstract`x`JSBI.EQ(x,
Generic`x`JSBI.NE(x,
Generic`x`JSBI.LT(x,
Generic`x`JSBI.LE(x,
Generic`x`JSBI.GT(x,
Generic`x`JSBI.GE(x,
Generic`x`JSBI.ADD(x,

The variable names x and y here indicate that the variables can refer to anything, for example: JSBI.GT(101.5, BigInt('100')) or str = JSBI.ADD('result: ', BigInt('0x2A')).

Unfortunately, there are also a few things that are not supported at all:

UnsupportednativeJSBI
-----------------------------------------------------------------------
literals`aN/A
increment`a++`N/A
|`JSBI.add(a,
decrement`a--`N/A
|`JSBI.subtract(a,

It is impossible to replicate the exact behavior of the native ++ and -- operators in a polyfill/library. Since JSBI is intended to be transpiled away eventually, it doesn’t provide a similar-but-different alternative. You can use JSBI.add() and JSBI.subtract() instead.

Since version 4.2.0, polyfills for DataView operations are included (where dv is a DataView, i is an index, le is an optional boolean indicating little endian mode, and x is a BigInt or a JSBI instance, respectively):

nativeJSBI
|-----------------------------|-------------------------------------------|
`dv.getBigInt64(i,`JSBI.DataViewGetBigInt64(dv,
`dv.setBigInt64(i,`JSBI.DataViewSetBigInt64(dv,
`dv.getBigUint64(i,`JSBI.DataViewGetBigUint64(dv,
`dv.setBigUint64(i,`JSBI.DataViewSetBigUint64(dv,

When?


Now! The JSBI library is ready for use today.

Once BigInts are natively supported everywhere, use babel-plugin-transform-jsbi-to-bigint to transpile your JSBI code into native BigInt code once and for all.

View our issue tracker to learn more about out our future plans for JSBI, and please join the discussion!

A more vague future plan is to use the JSBI library (or an extension to it) as a staging ground for additional BigInt-related functionality. The official proposal is intentionally somewhat minimal, and leaves further “library functions” for follow-up proposals. Examples are a combined exp+mod function, and bit manipulation functions.

Development


1. Clone this repository and cd into the local directory.

1. Use the Node.js version specified in .nvmrc:

  1. ```sh
  2.      nvm use
  3. ```

1. Install development dependencies:

  1. ```sh
  2.     npm install
  3. ```

1. Run the tests:

  1. ```sh
  2.     npm test
  3. ```

    See npm run for the list of commands.

For maintainers


How to publish a new release


1. On the main branch, bump the version number in package.json:

  1. ```sh
  2.     npm version patch -m 'Release v%s'
  3. ```

    Instead of patch, use minor or major as needed.

    Note that this produces a Git commit + tag.

1. Push the release commit and tag:

  1. ```sh
  2.     git push && git push --tags
  3. ```

    Our CI then automatically publishes the new release to npm.