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
-
Should primitives be coerced with
ToObject(likeObject.keys) or should the method throw to surface bugs? -
Is the name
Object.isEmptyideal, or should we consider alternatives such asObject.isEmptyObject? -
What optimizations or exotic-object behaviors (e.g., proxies, host objects) should be specified explicitly?