1 of 8

Arbitrary precision BigInts in JavaScript:

Towards Stage 3

Daniel Ehrenberg

Igalia

2 of 8

Summary of feature

  • Single type for all integers
  • Literal syntax: 43539234598764325897635879n
  • Operator overloading: 1n + 2n === 3n
  • Exceptions on mixed types: 1n + 2 -> TypeError

3 of 8

Changes since last time

  • Name change to BigInt
    • Intuition: Keep using Number for small ints
  • Rounding Number->Integer throws when not safe
  • Throw on JSON.serialize (use a replacer)
  • Mixed comparison
  • Various typo/bug fixes
    • Coercion semantics

4 of 8

Proposed answers for questions

  • Mixed comparisons don’t throw
  • BigInt name, n literal suffix
  • toString returns number, no suffix
  • Very minimal standard library
    • No bitcasting Number <-> BigInt
    • No BigInt Uint8Array, etc

5 of 8

Mixed comparison semantics

  • 1n == null -> false
  • 1n == undefined -> false
  • 1n == 1 -> true
  • 1n == 0 -> false
  • 1n == “1” -> true
  • 1n == “0” -> false
  • 1n == “hello” -> SyntaxError
  • 1n == Object(1n) -> true
  • 1n == true -> true
  • 1n == false -> false
  • 1n == Symbol() -> false
  • 1n < 1 -> false

6 of 8

Considered integrated type

  • Axel Rauschmayer’s proposal
    • Number has integers (1) and doubles (1.0)
    • Mixed operations upcast to doubles
    • With two integers, (sometimes) do integer arithmetic semantics
  • Upside: Matches some developers’ intuition
  • Downsides:
    • Implicitly lose precision
    • Might break the web (example: multiply by 2 until Infinity)
    • Would need a -0 integer
    • Unclear how to extend to more types

7 of 8

Status

  • Stage 2
  • Text pretty stable from last meeting
  • Igalia is working on implementations
  • Stage 3?

8 of 8

References