Object.keysLength S2
- Stage: Stage 2
- Status: Active
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces Object.keysLength, a built-in method that returns the count of an object's own enumerable string-keyed properties, equivalent to Object.keys(obj).length but without allocating an intermediate array. It aims to reduce memory overhead and improve performance in hot paths where this pattern is frequently used.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
ECMAScript Proposal: Object.keysLength
Note: This proposal was split out of the broader Object Property Counting effort.
For the wider discussion and a more general API that counts different kinds of own properties, see the parent proposal: https://github.com/tc39/proposal-object-property-count
Status
- Stage: 2
- Champions: Ruben Bridgewater, Jordan Harband
- Authors: Ruben Bridgewater, Jordan Harband
You can browse the spec text at https://tc39.es/proposal-object-keys-length/ or view the source.
Overview
Object.keysLength(target) returns the number of an object’s own enumerable string-keyed properties — precisely the same set of properties returned by Object.keys(target), but without allocating the intermediate array. In other words:
This narrowly scoped API addresses the single most common “count properties” pattern in the wild: Object.keys(obj).length.
Motivation
Developers frequently write Object.keys(obj).length to determine how many own enumerable string-keyed properties an object has. While clear, this forces the engine to create an array of keys only to immediately take its .length, incurring avoidable allocation and garbage-collection overhead. This cost can be significant on hot paths and in frameworks and libraries that perform this check repeatedly.
Providing a built-in that returns the same information without producing an intermediate array:
- avoids needless allocation and GC pressure,
- makes intent explicit and readable, and
- enables engines to optimize the common case directly.
This proposal intentionally focuses on the exact semantics of Object.keys (own, enumerable, string-keyed properties). Broader counting needs (e.g., including symbols or non-enumerables, or choosing among key types) are covered by the parent proposal, Object.propertyCount.
Proposed API
- Parameters:
target— coerced withToObject(likeObject.keys). Passingnullorundefinedthrows aTypeError. - Return value: a non-negative integer equal to
Object.keys(target).length.
See the spec for the precise algorithm.
Examples
Basic objects:
Objects without a prototype:
Arrays and sparse arrays (counts present indices only):
Symbols are not counted (matching Object.keys):
Non-enumerable properties are not counted:
Primitives are coerced like Object.keys:
Error cases:
Relationship to Object.propertyCount
Object.keysLength is a focused subset of the larger Object.propertyCount proposal. While Object.keysLength is fixed to match Object.keys semantics (own, enumerable, string-keyed properties), Object.propertyCount explores a flexible API that can also count symbols and/or non-enumerables via options. If your use case needs those capabilities, see the parent proposal: https://github.com/tc39/proposal-object-property-count.
Prior art and usage
The pattern Object.keys(obj).length is ubiquitous in production codebases across frameworks and libraries (e.g., React, Angular, Vue, Lodash, Node.js, Storybook, VS Code, and many more). This API makes that common intent fast without changing semantics.
Links
- Spec text: https://tc39.es/proposal-object-keys-length/
- Spec source: spec.emu
- Parent proposal: https://github.com/tc39/proposal-object-property-count