How to efficiently count the number of keys/properties of an object in JavaScript ?

To efficiently count the number of keys/properties in a JavaScript object, use Object.keys() combined with the length property. This method is concise, optimized for performance, and works for most use cases:

const obj = { a: 1, b: 2, c: 3 };
const count = Object.keys(obj).length; // 3

Key Methods & Scenarios

MethodUse CaseIncludes Inherited?Includes Symbols?Includes Non-Enumerable?
Object.keys(obj).lengthMost common: Count own enumerable keys
Object.getOwnPropertyNames(obj).lengthIncludes non-enumerable keys (e.g., defined with Object.defineProperty)
Reflect.ownKeys(obj).lengthIncludes symbols and all own keys
for...in + counterRarely needed (inherited keys, but requires hasOwnProperty checks)✅*

Detailed Breakdown

1. Object.keys(obj).length (Recommended)

  • Efficiency: Fastest method (native implementation).
  • Behavior: Returns only own enumerable string-keyed properties.
  const obj = { a: 1, b: 2 };
  Object.defineProperty(obj, 'hidden', { value: 3, enumerable: false });
  console.log(Object.keys(obj).length); // 2 (ignores 'hidden')

2. Object.getOwnPropertyNames(obj).length

  • Use Case: Count all own properties (including non-enumerable ones).
  const obj = { a: 1 };
  Object.defineProperty(obj, 'hidden', { value: 2, enumerable: false });
  console.log(Object.getOwnPropertyNames(obj).length); // 2

3. Reflect.ownKeys(obj).length

  • Use Case: Includes symbol keys and all own properties.
  const sym = Symbol('key');
  const obj = { [sym]: 1, a: 2 };
  console.log(Reflect.ownKeys(obj).length); // 2

4. for...in Loop (Not Recommended for Efficiency)

  • Use Case: Rare scenarios requiring inherited keys (with checks).
  let count = 0;
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) count++;
  }
  console.log(count);

Performance Comparison

MethodSpeed (Relative)Memory Use
Object.keys().lengthFastest ✅Low
Reflect.ownKeys().lengthSlightly slowerModerate
for...in + counterSlowestLow

Edge Cases

  • Empty Object: Returns 0.
  • null/undefined: Throws an error. Guard with optional chaining:
  const count = Object.keys(obj || {}).length;
  • Symbols: Use Reflect.ownKeys() if symbols are present.

Example Workflow

// Example object with mixed properties
const obj = { a: 1 };
Object.defineProperty(obj, 'hidden', { value: 2, enumerable: false });
const sym = Symbol('secret');
obj[sym] = 3;

// Count enumerable string keys
console.log(Object.keys(obj).length); // 1 ('a')

// Count all own string keys (including non-enumerable)
console.log(Object.getOwnPropertyNames(obj).length); // 2 ('a', 'hidden')

// Count all own keys (including symbols)
console.log(Reflect.ownKeys(obj).length); // 3 ('a', 'hidden', Symbol(secret))

Final Recommendation

Use Object.keys(obj).length for 99% of cases. For advanced scenarios (symbols or non-enumerable properties), use Reflect.ownKeys() or Object.getOwnPropertyNames(). Avoid for...in unless explicitly handling inherited properties.

Leave a Reply

Your email address will not be published. Required fields are marked *