Why Large IDs Break in JavaScript: MAX_SAFE_INTEGER and 64-bit JSON

Why JSON.parse turns 1234567890123456789 into 1234567890123456800, which IDs and timestamps it breaks, and how to send and parse 64-bit integers safely.

A backend returns an order with the ID 1234567890123456789. The frontend parses the response, and the ID it gets back is 1234567890123456800. There is no error and no warning. The number has the right number of digits and looks plausible, and the next request built from it returns 404 for an order that exists.

This is not a bug in any one library. It is how numbers work in JavaScript, and it affects every 64-bit integer that passes through JSON.parse. Paste a large value into the converter below to see its exact binary and hex form; anything that needs more than 53 bits is at risk.

Binary ConverterConvert a number between binary, octal, decimal and hexadecimal, or convert text to binary and back. Values stay exact at any size, including well beyond 64 bits.

The limit is 2^53, not 2^64

JavaScript has one number type for everyday use, Number, and it is an IEEE 754 double-precision float. A double stores 53 significant bits, so it can represent every integer exactly only up to 2^53 − 1:

Number.MAX_SAFE_INTEGER       // 9007199254740991
2 ** 53                       // 9007199254740992
2 ** 53 + 1                   // 9007199254740992, the same number
9007199254740992 === 9007199254740993  // true

Above that limit, doubles can still represent huge numbers, but with gaps between them. Between 2^53 and 2^54 only even integers exist. Between 2^54 and 2^55 only multiples of 4 exist, and the gap keeps doubling. Any integer that falls in a gap is rounded to the nearest one that exists.

Our ID needs 61 bits. At that size the gap between representable numbers is 256, so the value is rounded to 1234567890123456768. JavaScript then prints it as 1234567890123456800, the shortest decimal that rounds to the same double.

The rounding also merges different IDs:

1234567890123456789 === 1234567890123456800  // true
1234567890123456789 === 1234567890123456700  // true

Every integer within 128 of ...768 becomes the same number. Two different orders can end up with the same ID in your frontend, which is worse than a crash.

Why JSON does not warn you

The JSON grammar allows numbers of any size, so {"id": 1234567890123456789} is valid JSON. What happens when it is parsed is left to the implementation. RFC 8259, the JSON standard, notes that only numbers within the range and precision of IEEE doubles can be expected to work everywhere.

JSON.parse follows that: it reads every number into a Number, rounding as needed. A Java, Go, Rust or C# service can serialize an int64 correctly, and a Python service can serialize an integer of any size, and the value is still corrupted the moment JavaScript parses the response.

Where you will meet it

  • Database IDs. PostgreSQL bigint and MySQL BIGINT columns are 64-bit. Auto-increment IDs stay small for a long time, but IDs from a sharded or distributed generator do not.
  • Snowflake-style IDs. Twitter (now X), Discord and many internal systems generate 64-bit IDs that combine a timestamp, a machine number and a counter. They are around 2^60 from the start, far past the safe limit.
  • High-resolution timestamps. Unix time in milliseconds is safe for a very long time, but in nanoseconds it is not. The current time in nanoseconds is around 1.8 × 10^18, about 200 times the safe limit.
  • Protobuf and gRPC. int64 and uint64 fields have the same problem in JavaScript, which is why the protobuf JSON mapping writes them as strings.
  • Hashes and bit flags stored as 64-bit integers, such as xxHash or FNV values.

The symptom is always the same: the last two or three digits turn into zeros, or a value close to them.

How to fix it

1. Send large integers as strings

This is the fix that works everywhere. Change the API to send "id": "1234567890123456789" with quotes, and treat IDs as opaque strings in the frontend. You almost never do arithmetic on an ID, so a string loses nothing.

Large public APIs do exactly this. Twitter's v1 API sent both id and id_str for every object because so many clients were affected, and Discord sends all its snowflake IDs as strings. Most serializers have a setting for it, such as Jackson's ToStringSerializer in Java or the ,string struct tag option in Go.

2. Parse the original digits

If you cannot change the API, read the number before JSON.parse rounds it. Recent JavaScript engines pass the original source text to the reviver function as a third argument:

const order = JSON.parse(body, (key, value, context) =>
  key === 'id' ? BigInt(context.source) : value,
);
order.id  // 1234567890123456789n

This is a recent addition to the language, so check that it is supported everywhere your code runs. In older environments, a parser library such as json-bigint or lossless-json does the same job.

3. Serialize BigInt deliberately

BigInt holds integers of any size exactly, but JSON.stringify refuses to guess how to write one:

JSON.stringify({ id: 1234567890123456789n })
// TypeError: Do not know how to serialize a BigInt

Pick a format and say so with a replacer. Writing it as a string matches fix 1:

JSON.stringify(order, (key, value) =>
  typeof value === 'bigint' ? value.toString() : value,
);
// {"id":"1234567890123456789"}

Newer engines also have JSON.rawJSON, which writes the digits as a bare number. Only use that if every consumer of the JSON can parse it without rounding.

Checking whether a value is safe

Number.isSafeInteger() tells you whether a number is within the exact range:

Number.isSafeInteger(9007199254740991)  // true
Number.isSafeInteger(9007199254740992)  // false

It can only check a number that has already been parsed, so it cannot recover a rounded ID. It is still useful as an assertion: if a field that should be an exact integer fails the check, the data is already wrong, and failing loudly is better than sending a request for the wrong record.

Database drivers often handle this for you. node-postgres, for example, returns bigint columns as strings by default rather than risk rounding them. If you change that setting to return numbers, you bring the problem back.

Quick reference

SituationWhat to do
You control the APISend 64-bit integers as JSON strings
You cannot change the APIParse with context.source in the reviver, or a lossless JSON library
Holding the value in JavaScriptUse a string for IDs, or BigInt if you need arithmetic
Writing BigInt to JSONUse a replacer that calls toString()
Checking a parsed numberNumber.isSafeInteger(); a false means it may already be rounded
The safe limit2^53 − 1 = 9,007,199,254,740,991

Try the tools