Number.MAX_SAFE_INTEGER + 1 and + 2 Are the Same Number

2026-12-05来源 dev.toEnglish

Number.MAX_SAFE_INTEGER is 9007199254740991. Add one and you get 9007199254740992. Add two and you get 9007199254740992 again, and the two results compare equal.

That is not a printing quirk. They are the same number.

console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 console.log(Number.MAX_SAFE_INTEGER + 1); // 9007199254740992 console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2); // true 

BigInt does not have this problem:

console.log(BigInt(Number.MAX_SAFE_INTEGER) + 2n); // 9007199254740993n 

Why +1 and +2 land on the same value

JavaScript numbers are IEEE 754 double-precision floats. A double has 53 bits of mantissa, so every integer up to 2^53 can be stored exactly. MAX_SAFE_INTEGER is 2^53 - 1, the largest integer where x + 1 !== x still holds and every integer below it is representable.

Above 2^53 the spacing between representable numbers is no longer 1. At 2^53 the step is 2, at 2^54 it is 4, and it doubles again every time the exponent grows. So 9007199254740993 is not a number a double can hold. It rounds to the nearest representable value, 9007199254740992, which is exactly where +1 already landed.

One more detail worth checking yourself: Number.MAX_SAFE_INTEGER + 2 - Number.MAX_SAFE_INTEGER comes back as 1. You added 2, but the addition lost it before the subtraction ever ran.

How to catch it

Number.isSafeInteger is the direct test:

Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false Number.isSafeInteger(9007199254740993); // false 

Note the last line: the literal 9007199254740993 is already rounded before isSafeInteger sees it, so it reports false, but the value it received is 9007199254740992.

Where this actually bites

  • Database ids. 64-bit ids (Postgres bigint, Snowflake-style ids, some UUID-as-integer schemes) routinely exceed 2^53. JSON.parse turns them into doubles and silently rounds them, so two different rows can come back with the same id, and a lookup by that id misses.
  • Nanosecond timestamps. Date.now() is milliseconds and safe for the next few hundred thousand years. process.hrtime.bigint() returns nanoseconds and crosses 2^53 within months of process start.
  • Counters and hashes. A 64-bit counter or hash compared as a number can collide for no visible reason.

The fix is the same in all three cases: keep the value as a string or a BigInt from the moment it enters the process, and only convert to a number if you have checked that it fits.

  • Send ids as JSON strings, and parse them as strings.
  • For JSON.parse, use a reviver or a big-number library so integers above the safe range do not become doubles.
  • Use BigInt for arithmetic on large integers, and Number.isSafeInteger as an assertion at the boundary where a value becomes a number.

The takeaway

MAX_SAFE_INTEGER is not a limit on the size of numbers you can write. It is the point where the gap between consecutive integers stops being 1. Past it, two different integers can be the same double, and no amount of care in the comparison code will tell them apart.

Tested on Node.js 22. The narration in the video is an AI-generated voice; the output above is from a real run.

30-second version: https://www.youtube.com/shorts/wlejqLVcQl4

原文:https://www.a8q9.cn/en/article/number-max-safe-integer-1-and-2-are-the-same-number-f8131e.html

相关文章