Array.prototype.size

It would be conveniently consistent if collection-like objects could all return their number of items from a size property.

set = Set([
  'What did Yoda say when he saw himself in 4K?',
  'HDMI.'
])
arr = Array.from(set)
arr.size === set.size // 2 true it is

Downsides from my limited perspective:

  • Two properties (length and size) for the same thing.
  • Possible conflicts with ancient user land libraries?
  • Derails false-etamology-like explanations I’ve invented to justify the difference outside “because time and Java”.

Alternatives from my limited perspective:

  • Something like Symbol.size that does the same thing, tho it may offer little over a userland helper pattern.

I did some digging into old archives and found some answers:

  • Two properties (length and size) for the same thing.

length was explicitly reserved for indexed types and is explicitly used by the array-like protocol (for Array.from, all the Array.prototype methods, and so on), so they're not quite the same thing.

  • Derails false-etamology-like explanations I’ve invented to justify the difference outside “because time and Java”.

The size property name was in fact derived from Java, though they obviously didn't just pull it directly without modification: Map/Set.prototype.size. Apparently, Firefox shipped a size method on their own in Aurora (their old nightly) leading up to that.