What Is the Upper Boundary for Lodash isLength
In the Lodash JavaScript library, the _.isLength utility
function determines whether a value is valid for use as the length
property of an array-like object. This article identifies the exact
numeric upper boundary accepted by _.isLength, breaks down
the validation criteria used under the hood, and explains why this
specific limit exists within the JavaScript runtime.
The exact upper boundary defining a valid array-like length in
Lodash's _.isLength is
9007199254740991 (\(2^{53} - 1\)). In modern JavaScript
environments, this value corresponds directly to the constant
Number.MAX_SAFE_INTEGER.
Validation Criteria of
_.isLength
For a value to pass the _.isLength check, it must
satisfy four distinct conditions:
- Type Check: The value must be of primitive type
number(typeof value == 'number'). - Non-Negative: The value must be greater than
-1(value > -1), meaning it must be zero or a positive number. - Integer Representation: The value must be a whole
number with no fractional component (
value % 1 == 0). - Upper Boundary: The value must not exceed
9007199254740991(value <= 9007199254740991).
Implementation Details
Lodash implements this function internally as follows:
const MAX_SAFE_INTEGER = 9007199254740991;
function isLength(value) {
return typeof value == 'number' &&
value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
}Why 9007199254740991
Is Used
JavaScript represents all numbers using the IEEE 754 standard for double-precision floating-point format. In this format, integers can only be safely and accurately represented without precision loss up to \(2^{53} - 1\).
Beyond 9007199254740991, JavaScript cannot guarantee
distinct sequential integer representation—for example,
9007199254740991 + 1 and 9007199254740991 + 2
can evaluate to the exact same value. Consequently, Lodash treats any
number greater than this threshold as unsafe to serve as a collection
length.
Boundary Behavior Examples
_.isLength(0)returnstrue(minimum valid boundary)._.isLength(9007199254740991)returnstrue(maximum valid boundary)._.isLength(9007199254740992)returnsfalse(exceeds upper boundary)._.isLength(Infinity)returnsfalse(exceeds upper boundary)._.isLength(-1)returnsfalse(below lower boundary)._.isLength(3.14)returnsfalse(fails integer check).