GoogleChromeLabs / Jsbi

Licence: apache-2.0
JSBI is a pure-JavaScript implementation of the official ECMAScript BigInt proposal.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jsbi

Loading Attribute Polyfill
Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.
Stars: ✭ 335 (-49.47%)
Mutual labels:  polyfill
Object Fit Polyfill
A Javascript polyfill for browsers that don't support the object-fit CSS property.
Stars: ✭ 493 (-25.64%)
Mutual labels:  polyfill
Css Paint Polyfill
CSS Custom Paint / Paint Worklet polyfill with special browser optimizations.
Stars: ✭ 575 (-13.27%)
Mutual labels:  polyfill
Polyfill Ctype
This component provides a partial, native PHP implementation for the Ctype extension.
Stars: ✭ 3,774 (+469.23%)
Mutual labels:  polyfill
React Lifecycles Compat
Backwards compatibility polyfill for React class components
Stars: ✭ 457 (-31.07%)
Mutual labels:  polyfill
Resize Observer
Polyfills the ResizeObserver API.
Stars: ✭ 540 (-18.55%)
Mutual labels:  polyfill
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+469.98%)
Mutual labels:  polyfill
Polyfill Apcu
This component provides apcu_* functions and the APCUIterator class to users of the legacy APC extension.
Stars: ✭ 629 (-5.13%)
Mutual labels:  polyfill
Sugar
A Javascript library for working with native objects.
Stars: ✭ 4,457 (+572.25%)
Mutual labels:  polyfill
Unfetch
🐕 Bare minimum 500b fetch polyfill.
Stars: ✭ 5,239 (+690.2%)
Mutual labels:  polyfill
Avif.js
AVIF polyfill for the browser
Stars: ✭ 399 (-39.82%)
Mutual labels:  polyfill
Webappsec Trusted Types
A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
Stars: ✭ 424 (-36.05%)
Mutual labels:  polyfill
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (-14.63%)
Mutual labels:  polyfill
Fakeindexeddb
A pure JS in-memory implementation of the IndexedDB API
Stars: ✭ 373 (-43.74%)
Mutual labels:  polyfill
Bootstrap Ie7
Bootstrap 3 CSS for IE7
Stars: ✭ 578 (-12.82%)
Mutual labels:  polyfill
Css3 Mediaqueries Js
CSS3 Media Queries Shim
Stars: ✭ 333 (-49.77%)
Mutual labels:  polyfill
Undom
🍩 1kb minimally viable DOM Document implementation
Stars: ✭ 496 (-25.19%)
Mutual labels:  polyfill
Text Encoding
Polyfill for the Encoding Living Standard's API
Stars: ✭ 629 (-5.13%)
Mutual labels:  polyfill
Polyfill Service
Automatic polyfill service.
Stars: ✭ 5,585 (+742.38%)
Mutual labels:  polyfill
Gray
Make an image grayscale in all browsers
Stars: ✭ 568 (-14.33%)
Mutual labels:  polyfill

JSBI — pure-JavaScript BigInts Build status

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

Installation

npm install jsbi --save

Usage

import JSBI from './jsbi.mjs';

const max = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
console.log(String(max));
// → '9007199254740991'
const other = JSBI.BigInt('2');
const result = JSBI.add(max, other);
console.log(String(result));
// → '9007199254740993'

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+) and Node.js (v10.4+), and they are expected to come to other browsers in the future — which means you can't use them yet if you want your code to run everywhere.

To use BigInts in your code today, 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 will when they become available, so to 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.

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 replace BigInt with JSBI.BigInt:

Operation native BigInts JSBI
Creation from String a = BigInt('456') a = JSBI.BigInt('456')
Creation from Number a = BigInt(789) a = JSBI.BigInt(789)
Conversion to String a.toString(radix) a.toString(radix)
Conversion to Number Number(a) JSBI.toNumber(a)
Truncation BigInt.asIntN(64, a) JSBI.asIntN(64, a)
BigInt.asUintN(64, a) JSBI.asUintN(64, a)
Type check typeof a === 'bigint' a instanceof JSBI

Most operators are replaced by method calls:

Operation native BigInts JSBI
Addition c = a + b c = JSBI.add(a, b)
Subtraction c = a - b c = JSBI.subtract(a, b)
Multiplication c = a * b c = JSBI.multiply(a, b)
Division c = a / b c = JSBI.divide(a, b)
Remainder c = a % b c = JSBI.remainder(a, b)
Exponentiation c = a ** b c = JSBI.exponentiate(a, b)
Negation b = -a b = JSBI.unaryMinus(a)
Bitwise negation b = ~a b = JSBI.bitwiseNot(a)
Left shifting c = a << b c = JSBI.leftShift(a, b)
Right shifting c = a >> b c = JSBI.signedRightShift(a, b)
Bitwise “and” c = a & b c = JSBI.bitwiseAnd(a, b)
Bitwise “or” c = a | b c = JSBI.bitwiseOr(a, b)
Bitwise “xor” c = a ^ b c = JSBI.bitwiseXor(a, b)
Comparison to other BigInts a === b JSBI.equal(a, b)
a !== b JSBI.notEqual(a, b)
a < b JSBI.lessThan(a, b)
a <= b JSBI.lessThanOrEqual(a, b)
a > b JSBI.greaterThan(a, b)
a >= b JSBI.greaterThanOrEqual(a, b)

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:

Operation native BigInts JSBI
Abstract equality comparison x == y JSBI.EQ(x, y)
Generic “not equal” x != y JSBI.NE(x, y)
Generic “less than” x < y JSBI.LT(x, y)
Generic “less than or equal” x <= y JSBI.LE(x, y)
Generic “greater than” x > y JSBI.GT(x, y)
Generic “greater than or equal” x >= y JSBI.GE(x, y)
Generic addition x + y JSBI.ADD(x, y)

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:

Unsupported operation native BigInts JSBI
literals a = 123n; N/A ☹
increment a++ N/A ☹
a + 1n JSBI.add(a, JSBI.BigInt('1'))
decrement a-- N/A ☹
a - 1n JSBI.subtract(a, JSBI.BigInt('1'))

It is impossible to replicate the exact behavior of the native ++ and -- operators with static functions. 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.

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.

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

    nvm use
    
  3. Install development dependencies:

    npm install
    
  4. Run the tests:

    npm test
    

    See npm run for the list of commands.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].