# Iterator helper: length()

**URL:** <https://es.discourse.group/t/iterator-helper-length/2476>\
**Category:** 💡 Ideas\
**Created:** [December 15, 2025, 6:25pm UTC](https://es.discourse.group/t/iterator-helper-length/2476 "2025-12-15T18:25:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hydroperfox](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/hydroperfox/32/2233_2.png) [@hydroperfox](https://es.discourse.group/u/hydroperfox)\
**Post date:** [December 15, 2025, 6:25pm UTC](https://es.discourse.group/t/iterator-helper-length/2476/1 "2025-12-15T18:25:33Z")

</div>

Should be roughly the same as Rust’s `count()` method.

```javascript
Iterator.prototype.length = function() { let n = 0; for (const val of this) n++; return n };

```

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [December 15, 2025, 6:26pm UTC](https://es.discourse.group/t/iterator-helper-length/2476/2 "2025-12-15T18:26:50Z")

</div>

`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?

---

<div class="post-metadata">

**Author:** ![hydroperfox](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/hydroperfox/32/2233_2.png) [@hydroperfox](https://es.discourse.group/u/hydroperfox)\
**Post date:** [December 15, 2025, 6:32pm UTC](https://es.discourse.group/t/iterator-helper-length/2476/3 "2025-12-15T18:32:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 15, 2025, 11:05pm UTC](https://es.discourse.group/t/iterator-helper-length/2476/4 "2025-12-15T23:05:48Z")

</div>

If getting the count consumes the iterator, then you can't ever get the items - in a general sense, iterators aren't reusable.
