How Lodash Determines Arguments Object Size

The Lodash JavaScript library determines the size of an arguments object by classifying it as an "array-like" collection and directly reading its native length property. Instead of iterating over its enumerable properties or converting it into an array, Lodash uses lightweight internal type-checking functions to verify that the object has a valid length property, allowing it to return the count in constant \(O(1)\) time.

The Role of _.size

When you pass an arguments object to Lodash’s size() function, the library evaluates the data type using an internal hierarchy. The core implementation of size() functions conceptually as follows:

  1. Null Check: If the input is null or undefined, it immediately returns 0.
  2. Array-Like Check: It passes the target to isArrayLike().
  3. Length Retrieval: If isArrayLike() returns true, it returns the object's .length property (with a special case only for unicode strings).
  4. Fallback: If not array-like, it checks for Map or Set structures (reading .size), or counts object keys via Object.keys().

Because an arguments object qualifies under the isArrayLike check, Lodash bypasses standard object key enumeration entirely.

How isArrayLike Identifies the arguments Object

In JavaScript, the arguments object is not a true array, but it shares key characteristics with one. Lodash identifies it using isArrayLike(value), which checks two conditions:

The isLength() helper verifies that the length property is an integer greater than or equal to 0 and less than or equal to Number.MAX_SAFE_INTEGER (\(2^{53} - 1\)). Because JavaScript automatically populates arguments.length with the number of parameters passed to the function, the arguments object passes this validation.

Why Lodash Does Not Use isArguments for Sizing

Lodash contains a specialized isArguments() utility that inspects the internal [[Class]] tag (checking for [object Arguments]) to distinguish arguments from standard objects. However, _.size() does not need to invoke isArguments().

Differentiating between a real Array, an arguments object, or a DOM NodeList is unnecessary when the goal is simply counting items. Treating all of these as generic array-like structures allows Lodash to extract the count instantly via collection.length without the performance overhead of tag inspection or property iteration.