Object.isEmpty

I'd prefer this kind of semantics for an Object.isEmpty - it's simple and everyone interprets it as that low-level primitive. It also fits in with the rest:

  • Object.keys(new Map([["a", 1], ["b", 2]])) returns [], not ["a", "b"].
  • Object.values(new Map([["a", 1], ["b", 2]])) returns [], not [1, 2].
  • Object.entries(new Map([["a", 1], ["b", 2]])) returns [], not [["a", 1], ["b", 2]].

Better might be just Object.size(o), which returns effectively Object.keys(o).length. I've personally have had a couple cases where the length was useful independent of it, and it's easy enough to check Object.size(o) === 0 or !Object.size(o) for Object.isEmpty(o). And in nearly all runtimes, this is a very cheap O(1) access for non-proxies, cheaper than even property access.

2 Likes