BigInt64Array Stores Exact 64-Bit Integers
Store signed 64-bit integers beyond MAX_SAFE_INTEGER in BigInt64Array without float rounding loss, and inspect raw bytes with DataView.getBigInt64.
Code
// Number loses precision above MAX_SAFE_INTEGER (2^53 - 1 = 9007199254740991)
const unsafeBase = 9007199254740992;
console.log('float64 collapsed :', unsafeBase === unsafeBase + 1);
// BigInt64Array stores exact signed 64-bit integers (-2^63 .. 2^63 - 1)
const counters = new BigInt64Array([
9007199254740992n,
9007199254740995n,
-4200000000000000000n,
]);
counters[0] += 1n;
console.log('exact increment :', counters[0], 'changed?', counters[0] !== 9007199254740992n);
console.log('signed negative :', counters[2]);
// Inspect the shared ArrayBuffer using DataView with explicit endianness
const view = new DataView(counters.buffer, counters.byteOffset, counters.byteLength);
console.log('byteLength :', counters.byteLength, 'slot bytes:', BigInt64Array.BYTES_PER_ELEMENT);
console.log('little-endian [1] :', view.getBigInt64(8, true));
console.log('big-endian [1] :', view.getBigInt64(8, false));
// Overflow wraps modulo 2^64 in two's complement
const maxPlusOne = new BigInt64Array([0x7fff_ffff_ffff_ffffn + 1n]);
console.log('signed 64-bit wrap:', maxPlusOne[0]);Output
Captured from a sandboxed run of this snippet in the build pipeline.
float64 collapsed : true
exact increment : 9007199254740993n changed? true
signed negative : -4200000000000000000n
byteLength : 24 slot bytes: 8
little-endian [1] : 9007199254740995n
big-endian [1] : 216172782113792000n
signed 64-bit wrap: -9223372036854775808nInside the routine — BigInt64Array Stores Exact 64-Bit Integers
Demo state sits in top-level bindings: unsafeBase, counters, view, maxPlusOne.
What to watch for
- A stock language runtime is the sole requirement — zero external packages or installation.
Applying it
Output lines are prefixed (float64 collapsed :, exact increment :, signed negative :), allowing terminal logs to reveal each branch decision during execution.
Statements that matter
const maxPlusOne = new BigInt64Array([0x7fff_ffff_ffff_ffffn + 1n]);
counters[0] += 1n;
Interactive Tools for This Topic
FAQ
- Are there third-party dependencies?
- No. The dependency list is empty; the code stays inside the standard library. Its state lives in module-level bindings: `unsafeBase`, `counters`, `view`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.