Arbitrary precision Integers in JavaScript:
For Stage 2
Daniel Ehrenberg
Igalia
Why do anything?
Why Integer rather than Int64?
How?
1n + 2n
0xffn < 0x100n
let x = 1n; x++;
Code sample
// Takes a Integer as an argument and returns a Integer�function nthPrime(nth) {� function isPrime(p) {� for (let i = 2n; i < p; i++) {� if (p % i === 0n) return false;� }� return true;� }� for (let i = 2n; ; i++) {� if (isPrime(i)) {� if (--nth === 0n) return i;� }� }�}
Code sample: asm.js (?)
function Add64Module(stdlib, foreign, buffer) {� "use asm";� var cast = stdlib.Integer.asUintN;� var values = new stdlib.Uint64Array(buffer);� function add64(aIndex, bIndex) {� aIndex = aIndex|0;� bIndex = bIndex|0;� var aValue = values[aIndex>>3];� var bValue = values[bIndex>>3];� return cast(64, aValue + bValue);� }� return { add64: add64 };�}
Library features
No implicit coercion
No implicit coercion
Optimization potential
Specification status
References
Backup slides
Comparison semantics:
current proposal
1 < 1n TypeError
1 == 1n TypeError
1 === 1n false
Comparison semantics:
Allow semantic comparison
1 < 1n false
1 == 1n true
1 === 1n false