Proposal: `Object.isEmpty(target)`

Add Object.isEmpty(value) to return true only when value has zero own string or symbol keys (otherwise false, primitives throw). The repository contains the reference implementation, polyfill helper, documentation, and tests to explore adding this built-in to JavaScript.

Motivation and examples

  • Avoid verbose patterns (Object.keys(obj).length === 0, etc.) that miss symbol keys or objects without prototypes.

  • Provide a clear primitive that engines can optimize, aligning with other Object.* utilities. *

js
Object.isEmpty({}); // true
Object.isEmpty({ a: 1 }); // false
Object.isEmpty(Object.create(null)); // true
Object.isEmpty(Symbol("x")); // TypeError

Repository link
https://github.com/geekdroid07/object-is-empty

Open questions for feedback

  1. Should primitives be coerced with ToObject (like Object.keys) or should the method throw to surface bugs?

  2. Is the name Object.isEmpty ideal, or should we consider alternatives such as Object.isEmptyObject?

  3. What optimizations or exotic-object behaviors (e.g., proxies, host objects) should be specified explicitly?

1 Like

There is a brand new proposal for giving the number of keys, which would cover this. You may be interested in following along there.

1 Like