Skip to content

Reference for stage 3 iterator-chunking - #44884

Open
Josh-Cena wants to merge 1 commit into
mdn:mainfrom
Josh-Cena:iterator-chunking
Open

Reference for stage 3 iterator-chunking#44884
Josh-Cena wants to merge 1 commit into
mdn:mainfrom
Josh-Cena:iterator-chunking

Conversation

@Josh-Cena

Copy link
Copy Markdown
Member

Part of #44846

@Josh-Cena
Josh-Cena requested a review from a team as a code owner July 22, 2026 12:09
@Josh-Cena
Josh-Cena requested review from a team, hamishwillee and sideshowbarker and removed request for a team and sideshowbarker July 22, 2026 12:09
@github-actions github-actions Bot added Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed labels Jul 22, 2026
@github-actions

github-actions Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Preview URLs (3 pages)

External URLs (4)

URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/chunks
Title: Iterator.prototype.chunks()


URL: /en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/windows
Title: Iterator.prototype.windows()

(comment last updated: 2026-07-22 12:26:43)

sidebar: jsref
---

The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is something not quite right about "Each time" - i.e. it isn't clear that the thing being iterated here is the helper object. You could do

Suggested change
The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.
The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time the helper is iterated, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.

@hamishwillee hamishwillee Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also found the difference between non-overlapping an overlapping methods non-intuitive. I prefer this:

Suggested change
The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.
The **`chunks()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that splits the elements from the original iterator into consecutive array chunks. Each time the helper is iterated, it gets the specified number of elements from the underlying iterator and yields them together.


### Return value

A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array.
A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the original iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array.


A new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects). Each time the returned iterator helper's `next()` method is called, the current iterator is advanced by `chunkSize` elements, and those elements are yielded together as an array.

If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called.
If the original iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called.


If the current iterator has some but fewer than `chunkSize` elements remaining, those elements are still yielded as an array (so the length is less than `chunkSize`), and the iterator helper will be immediately completed the next time `next()` is called.

If the current iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the current iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array.
If the original iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array.

Comment on lines +10 to +12
The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields overlapping element sequences as arrays. A sliding window sweeps across the input iterator; each time, a new element is added to the right while the leftmost element is removed.

For yielding non-overlapping sequences (i.e., chunking), see {{jsxref("Iterator.prototype.chunks()")}}.

@hamishwillee hamishwillee Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem with my intuition in this case. I think this might be easier for most people to parse.

Suggested change
The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields overlapping element sequences as arrays. A sliding window sweeps across the input iterator; each time, a new element is added to the right while the leftmost element is removed.
For yielding non-overlapping sequences (i.e., chunking), see {{jsxref("Iterator.prototype.chunks()")}}.
The **`windows()`** method of {{jsxref("Iterator")}} instances returns a new [iterator helper object](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator#iterator_helper_objects) that yields a sliding window of elements as an array. On each iteration, the window slides forward by one element — yielding an array that drops the first element from the previous iteration and adds the next element from the original iterator.
To instead split elements into distinct chunks, see {{jsxref("Iterator.prototype.chunks()")}}.

## Instance methods

- {{jsxref("Iterator.prototype.chunks()")}}
- : Returns a new iterator helper object that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.

@hamishwillee hamishwillee Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about "each time" and overlapping vs non-overlapping as for the method docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:JS JavaScript docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants