Sub topic 13
64-Bit Integers with BigInt Typed Arrays
This chapter assumes you already know how typed arrays wrap out-of-range values and how DataView reads values at a given byte offset. You should also know the basics of BigInt: literals like 42n, and the fact that BigInt and Number cannot be mixed in arithmetic.
Why 64-bit integers need BigInt
A JavaScript Number is a 64-bit float. It holds every integer exactly only up to 2^53 − 1 (Number.MAX_SAFE_INTEGER). Binary formats often hold full 64-bit integers, such as nanosecond timestamps, file sizes, database IDs, and hashes. If you read one of these as a Number, the low bits can be lost without any error.
For this reason, the two 64-bit typed arrays store and return BigInt values, which have arbitrary precision:
| Type | Bytes per element | Range | Element value type |
|---|---|---|---|
| `BigInt64Array` | 8 | −2^63 to 2^63 − 1 | `BigInt` |
| `BigUint64Array` | 8 | 0 to 2^64 − 1 | `BigInt` |
The working model
The typed array works the same way as the other integer types. The only difference is at the boundary with JavaScript. Every read returns a BigInt, and every write must be a BigInt.
const big = new BigInt64Array(1);
big[0] = 2n ** 63n - 1n;
console.log(big[0]); // 9223372036854775807n
try {
big[0] = 1; // Number, not BigInt
} catch (e) {
console.log(e instanceof TypeError); // true
}Wrapping follows the rule you already know, applied modulo 2^64. It matches BigInt.asIntN(64, x) and BigInt.asUintN(64, x):
const u = new BigUint64Array(1);
u[0] = -1n;
console.log(u[0]); // 18446744073709551615n
const s = new BigInt64Array(1);
s[0] = 2n ** 63n;
console.log(s[0]); // -9223372036854775808nThe alignment rule also still applies. A BigInt64Array over an existing buffer needs a byteOffset that is a multiple of 8.
DataView methods
For unaligned fields or an explicit byte order, DataView has getBigInt64, setBigInt64, getBigUint64, and setBigUint64. They take the same littleEndian flag as the other getters and setters.
const dv = new DataView(new ArrayBuffer(9));
dv.setBigUint64(1, 9007199254740993n, true); // unaligned, little-endian
const id = dv.getBigUint64(1, true);
console.log(id); // 9007199254740993n
console.log(Number(id)); // 9007199254740992, precision lostCommon errors
- Building from Numbers.
new BigInt64Array([1, 2])andBigUint64Array.from([1, 2])both throw aTypeError. Write[1n, 2n], or map the values first:BigInt64Array.from([1, 2], BigInt). - Mixing in arithmetic.
big[0] + 1throws. Usebig[0] + 1n, or convert on purpose withNumber(...)once you know the value is within the safe range. - Number-only helpers.
Math.max(...big)throws. For a sum, startreducewith a BigInt seed:big.reduce((a, b) => a + b, 0n). The defaultsort()still orders the elements numerically. - Serializing.
JSON.stringifythrows onBigInt. Convert to a string first, for exampleArray.from(big, String). - Non-integer input to
BigInt().BigInt(1.5)throws aRangeError. Round the value before converting it.
Exercise
What does this code print, and why? const a = new BigUint64Array(2); a[0] = 2n ** 64n + 5n; a[1] = BigInt(Number.MAX_SAFE_INTEGER) + 2n; console.log(a[0], Number(a[1]));
Show answer
It prints 5n 9007199254740992. Writes wrap modulo 2^64, so 2^64 + 5 is stored as 5n. The second element is stored exactly as 9007199254740993n, but Number() rounds it to the nearest double, which is 9007199254740992.
Exercise
Why does new BigInt64Array(new ArrayBuffer(16), 4) throw, and what kind of error is it?
Show answer
It throws a RangeError. BigInt64Array elements are 8 bytes, so byteOffset must be a multiple of 8, and 4 is not. Use DataView.getBigInt64(4) to read an unaligned 64-bit field.