Should be roughly the same as Rust’s count() method.
Iterator.prototype.length = function() { let n = 0; for (const val of this) n++; return n };
Should be roughly the same as Rust’s count() method.
Iterator.prototype.length = function() { let n = 0; for (const val of this) n++; return n };
length would be a bad name because people expect it to be fast, but count is not totally unreasonable.
But it would need to have sufficient use cases. Why do you want this?
I thought of the case where you want to count Unicode Scalar Values from a string, like "ecmascript".chars().length().
I suggested length() instead of count() out of attraction to E4X's XML.length()/XMLList.length() method. Of course the name doesn't suggest well, I'd say... count() really seems like it will consume the iterator.
If getting the count consumes the iterator, then you can't ever get the items - in a general sense, iterators aren't reusable.