Skip to main content

Harshal V. LADHE

JavaScript Iterator chunks(), windows(), includes() and join()

Group, window, search and join any iterator natively.
Published at:
Last updated:
Estimated reading time:13 min read
Cover titled JavaScript Iterator chunks() & windows(), with four panels: chunks(3) splitting the digits 0 to 9 into [0, 1, 2], [3, 4, 5], [6, 7, 8] and a leftover [9]; windows(3) sliding over the same digits as [0, 1, 2], [1, 2, 3], [2, 3, 4] for 8 windows from 10 values; includes("failed") reading queued and running, matching failed and never reading the last queued; and join(", ") turning css, javascript and html into one string.(opens full-size image in a new tab)
Chunks, windows, an includes() that stops at the first match, and a join() straight to a string.

Introduction

Iterator helpers gave JavaScript map(), filter(), take() and friends for any iterator, not just arrays. Four everyday array tasks were still missing: splitting values into groups of n, looking at overlapping runs of n values, checking whether a value appears, and joining everything into a string. Each one meant converting to an array first or writing a loop by hand.

ES2027 adds all four to Iterator.prototype:

  • chunks(n) yields arrays of n values that don't overlap. The last one may be shorter.
  • windows(n) yields overlapping arrays of n values, moving forward one value at a time.
  • includes(value) returns true as soon as it finds the value, and stops reading.
  • join(separator) concatenates every value into a string, exactly like Array.prototype.join().

They come from three TC39 proposals, Iterator Chunking, Iterator Includes and Iterator Join, which have all reached stage 4 and are part of ES2027. For the rest of that edition, see What's New in ES2027: Temporal, using and More.

A Quick Iterator Helpers Refresher

An iterator is an object with a next() method that hands back { value, done } one value at a time. Arrays, Maps, Sets, strings and generators all give you one: array.values(), map.entries(), set.values(), or calling a generator function.

All of these built-in iterators inherit from Iterator.prototype, and since ES2025 it has array-like methods (covered in full in Iterator Helpers: Lazy Pipelines over Any Iterator):

const scores = [12, 7, 30, 18, 4];

const top = scores
  .values()                 // an iterator, not an array
  .filter((n) => n >= 10)
  .map((n) => n * 2)
  .toArray();               // [24, 60, 36]

Two things matter for the rest of this tutorial:

  • Helpers that return an iterator are lazy. filter() and map() don't do anything until something asks for values, such as toArray(), a for...of loop or spreading. Each value goes through the whole chain before the next one is read. That's why filter().take(n) stops at the nth match, as shown in First N Matches from Any Iterable with filter() and take().
  • Helpers that return a single answer consume the iterator. reduce(), some(), find() and toArray() read values until they have their answer. An iterator can only be read once, so after that it's used up.

chunks() and windows() belong to the first group: they return lazy iterators of arrays. includes() and join() belong to the second. For iterators that don't inherit from Iterator.prototype, such as a hand-written object with a next() method, Iterator.from(object) wraps it so the helpers work. Async iterators have their own collector, covered in Collecting Async Iterables into Arrays with Array.fromAsync().

chunks(n): Groups That Don't Overlap

const digits = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].values();

digits().chunks(3).toArray();
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

digits().chunks(5).toArray();
// [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

Every chunk is a new array of n values, except the last, which holds whatever is left over. An empty source yields no chunks at all, not one empty array.

The size has to be an integer from 1 to 232 − 1. Anything else throws straight away, before a single value is read, and closes the source iterator, so a generator passed a bad size is finished:

digits().chunks(0);    // RangeError: out of range
digits().chunks(2.5);  // TypeError: not an integer
digits().chunks("2");  // TypeError: strings aren't converted

The last line is easy to trip over: unlike many built-ins, chunks() doesn't convert its argument to a number. A size read from an <input> has to go through Number() first.

Because chunks() is lazy, it only ever holds one chunk in memory, so it works on huge or endless iterators too. With take(), you can ask for the first few chunks of something that never ends:

function naturals() {
  let n = 1;
  return Iterator.from({ next: () => ({ value: n++, done: false }) });
}

naturals().chunks(4).take(2).toArray(); // [[1, 2, 3, 4], [5, 6, 7, 8]]

windows(n): Overlapping Runs

digits().windows(3).toArray();
// [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]]

Each window drops the oldest value and adds the next one, so a source of length values gives length - n + 1 windows (when length is at least n). Every window is a full n values long, and each is a new array, so it's safe to keep or change one. The size follows the same rules as chunks(): an integer from 1 to 232 − 1, or a RangeError / TypeError.

When There Aren't Enough Values

If the source has fewer than n values, there's no full window, so by default windows() yields nothing. A second argument, undersized, changes that:

[1, 2].values().windows(3).toArray();                  // []
[1, 2].values().windows(3, "only-full").toArray();     // [] (the default)
[1, 2].values().windows(3, "allow-partial").toArray(); // [[1, 2]]

[1, 2, 3, 4].values().windows(3, "allow-partial").toArray();
// [[1, 2, 3], [2, 3, 4]]

"allow-partial" only makes a difference when the whole source is shorter than one window: then it yields a single window with every value. If there's at least one full window, the result is the same as the default, so it never adds short windows at the end. Any other value, apart from undefined, which means the default, throws a TypeError.

includes(value): Stop at the First Match

const statuses = ["queued", "running", "failed", "queued"];

statuses.values().includes("failed");   // true
statuses.values().includes("done");     // false

[1, NaN, 3].values().includes(NaN);     // true
[0].values().includes(-0);              // true

It compares values with SameValueZero, like Array.prototype.includes(): the same as ===, except that NaN matches NaN. Objects match only if they're the same object.

The difference from the array method is what happens to the source. includes() reads one value at a time and stops at the first match, closing the iterator. For a generator (a function* that pauses at each yield), that means its finally block runs and it won't produce anything else:

function* readLines() {
  try {
    yield "header";
    yield "ERROR: disk full";
    yield "footer"; // never reached
  } finally {
    console.log("closed");
  }
}

readLines().includes("ERROR: disk full");
// logs "closed", returns true

If there's no match, it reads every value before returning false. On an iterator that never ends, a value that never turns up means includes() never returns.

The Second Argument

Like the array method, includes() takes an optional second argument. For an iterator, it's the number of values to skip before comparing, so iterator.includes(x, 2) works like iterator.drop(2).includes(x):

const queue = () => ["queued", "running", "failed"].values();

queue().includes("queued", 1);  // false: the only "queued" was skipped
queue().includes("failed", 1);  // true

queue().includes("failed", -1); // RangeError: can't count from the end
queue().includes("failed", 1.5); // TypeError: not an integer

An iterator doesn't know its length, so there's no "count back from the end": negative numbers throw a RangeError. Non-integers throw a TypeError instead of being rounded, as they would be for an array. Infinity is allowed (it skips everything), but a finite count above 253 − 1 is a RangeError. As with a bad size, an invalid count closes the source before throwing.

join(separator): Straight to a String

const tags = new Set(["css", "javascript", "html"]);

tags.values().join(", "); // "css, javascript, html"
tags.values().join();     // "css,javascript,html"

join() follows Array.prototype.join() exactly:

  • With no separator (or undefined), it uses a comma.
  • Any other separator is converted to a string, so join(null) uses the text "null", just as [1, 2].join(null) gives "1null2".
  • null and undefined values become empty strings; every other value is converted to a string the way Array.prototype.join() does it, so a Symbol throws a TypeError.
  • An empty iterator gives "".
["a", null, undefined, 1].values().join(); // "a,,,1"
[].values().join("-");                     // ""

Before join(), the usual options were Array.from(iterator).join(sep), which builds a throwaway array, or reduce((a, b) => a + sep + b), which throws on an empty iterator. join() reads the values once and builds the string as it goes.

Real-World Uses

Batching API Requests

Many APIs cap how many IDs one request can take. chunks() turns a long list into requests of the right size:

async function fetchUsers(userIds) {
  const users = [];

  for (const batch of userIds.values().chunks(50)) {
    const response = await fetch(`/api/users?ids=${batch.join(",")}`);
    users.push(...(await response.json()));
  }

  return users;
}

The batches run one after another, which keeps the number of requests in flight to one. For 230 IDs that's five requests, the last one with 30 IDs. The batch.join(",") inside is the ordinary array method, since every chunk is an array.

Calendar and Grid Rows

A month view is a list of day cells cut into rows of seven. Pad the start with blanks for the weekday the month begins on, counting the first column as 0, then chunk:

function monthRows(startWeekday, daysInMonth) {
  const blanks = Array.from({ length: startWeekday }, () => null);
  const days = Array.from({ length: daysInMonth }, (_, i) => i + 1);

  return [...blanks, ...days]
    .values()
    .chunks(7)
    .map((week) => week.map((day) => (day === null ? "  " : String(day).padStart(2))).join(" "))
    .join("\n");
}

// startWeekday counts from the first column: 0 starts on it, 3 starts on the fourth
console.log(monthRows(3, 30));
//           1  2  3  4
//  5  6  7  8  9 10 11
// 12 13 14 15 16 17 18
// 19 20 21 22 23 24 25
// 26 27 28 29 30

The inner join(" ") is the array method on each week; the outer join("\n") is the new iterator method, so the rows go straight into one string without being collected first. The shorter last chunk is the last week of the month.

Moving Averages

A moving average is the mean of each window. windows() gives you the windows, and map() averages each one:

const dailyVisits = [3, 5, 7, 6, 8, 10];

const average = (values) => values.reduce((sum, n) => sum + n, 0) / values.length;

dailyVisits.values().windows(3).map(average).toArray();
// [5, 6, 7, 8]

Because everything is lazy, the same chain works on a live feed of readings: each average is ready as soon as its window is full, and only n values are held at a time.

Pairwise Differences

A window of two is a value and the one before it, which is all you need for changes between readings:

const balances = [100, 104, 101, 110];

balances.values().windows(2).map(([previous, current]) => current - previous).toArray();
// [4, -3, 9]

The same shape checks whether a list is sorted (.every(([a, b]) => a <= b)) or finds the first big jump (.find(([a, b]) => Math.abs(b - a) > 50)).

Try It Live

Type a list of values and a size to see chunks() and windows() side by side, with a moving average worked out from the windows. Try a size larger than the list, then switch undersized to "allow-partial", or enter 0 or 2.5 to see the errors. Below, includes() reports how many values it read before it stopped, and join() shows what happens to null and undefined.

The playground checks for each method separately. Where your browser has one natively it uses it; where it doesn't, the result comes from the fallbacks further down this page, and the method is clearly marked "fallback". In Chrome, for example, chunks() and windows() currently use the fallback.

Iterator Chunks, Windows, includes() and join() Playground

Type a list and a size to compare chunks() with windows(), switch windows() to "allow-partial", watch a moving average built from the windows, and see how many values includes() reads before it stops and what join() does with null and undefined, using each native method where the browser has it and a clearly labelled fallback where it doesn't.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Iterator Chunks, Windows, includes() and join() Playground</title>
  </head>
  <body>
    <main class="playground">
      <header class="playground-header">
        <h1>Chunks, Windows, includes() and join()</h1>
        <p>Type a comma-separated list and a size. <code>chunks(n)</code> splits it into groups that don't overlap, <code>windows(n)</code> slides over it one value at a time, and the windows feed a moving average. Below, <code>includes()</code> shows how far it reads before it stops, and <code>join()</code> builds a string.</p>
      </header>

      <p class="tip" id="support-tip" hidden>Methods marked "fallback" are missing in this browser, so those results come from the hand-written fallbacks in the tutorial, which follow the same rules. <code>chunks()</code> and <code>windows()</code> are native in Firefox 154; <code>join()</code> in Chrome and Edge 153 and Firefox 154; <code>includes()</code> in Chrome and Edge 154 and Firefox 154. Safari has all four in the 27.3 beta.</p>

      <ul class="status" id="status" aria-label="Native or fallback, per method"></ul>

      <fieldset class="control-group">
        <legend>Source values</legend>
        <div class="fields-grid">
          <div class="field field--full">
            <label for="values">Values <span>(NaN, null and undefined are kept as those values)</span></label>
            <input type="text" id="values" value="3, 5, 7, 6, 8, 10, 12" spellcheck="false" autocomplete="off" aria-describedby="parsed">
            <p class="hint" id="parsed"></p>
          </div>
          <div class="field">
            <label for="size">Chunk / window size</label>
            <input type="number" id="size" value="3" step="any">
          </div>
          <div class="field">
            <label for="undersized">windows() undersized</label>
            <select id="undersized">
              <option value="only-full">"only-full" (default)</option>
              <option value="allow-partial">"allow-partial"</option>
            </select>
          </div>
        </div>
      </fieldset>

      <section class="demo-area groups" aria-label="Chunks and windows">
        <div>
          <h2 class="group-title" id="chunks-title">chunks</h2>
          <ol class="rows" id="chunks-list"></ol>
        </div>
        <div>
          <h2 class="group-title" id="windows-title">windows</h2>
          <ol class="rows" id="windows-list"></ol>
        </div>
      </section>

      <output class="readout" id="groups-readout" aria-live="polite"></output>

      <fieldset class="control-group">
        <legend>includes() and join()</legend>
        <div class="fields-grid">
          <div class="field">
            <label for="search">includes() search value</label>
            <input type="text" id="search" value="6" spellcheck="false" autocomplete="off">
          </div>
          <div class="field">
            <label for="skip">Values to skip <span>(optional)</span></label>
            <input type="number" id="skip" placeholder="not passed" step="any">
          </div>
          <div class="field">
            <label for="separator">join() separator</label>
            <input type="text" id="separator" value=" | " spellcheck="false" autocomplete="off">
          </div>
        </div>
        <label class="checkbox-field">
          <input type="checkbox" id="default-separator">
          Call <code>join()</code> with no separator
        </label>
      </fieldset>

      <output class="readout" id="search-readout" aria-live="polite"></output>
    </main>

    <script src="./index.js"></script>
  </body>
</html>

Read-only
Ln –, Col –HTML3.7 KBUTF-8

Starting sandbox…

No console output yet.

Browser Support and Fallbacks

MethodChrome / EdgeFirefoxSafariNode.js
chunks() / windows()Not yet15427.3 (beta)Not yet
join()15315427.3 (beta)Not yet
includes()15415427.3 (beta)Not yet

Firefox is the only stable browser with all four, and Safari has them in the 27.3 beta. Chrome and Edge have join() and includes() but not the chunking methods, and Node.js 26 has none of them. The iterator helpers they build on are much older: Chrome and Edge 122, Firefox 131, Safari 18.4 and Node 22.

Detect each method on its own, because support differs between them:

const hasChunks = typeof Iterator !== "undefined" && typeof Iterator.prototype.chunks === "function";

Fallback Helpers

These plain functions follow the spec's rules, including closing the source when an argument is invalid, and take any iterable, so they work on arrays directly as well as on iterators. chunks() and windows() return hand-written lazy iterators (an object with next(), return() and Symbol.iterator), so they behave like the native versions in a for...of loop or a spread, and return() passes an early break on to the source. To chain helpers such as map() after them, wrap the result in Iterator.from().

const DONE = { done: true, value: undefined };

// The native methods close the source before throwing on a bad argument, so these do too.
function fail(source, error) {
  source.return?.();
  throw error;
}

function checkSize(source, size) {
  if (typeof size !== "number" || !Number.isInteger(size)) {
    fail(source, new TypeError("size must be an integer"));
  }
  if (size < 1 || size > 2 ** 32 - 1) {
    fail(source, new RangeError("size must be between 1 and 2^32 - 1"));
  }
}

function chunks(iterable, size) {
  const source = iterable[Symbol.iterator]();
  checkSize(source, size);
  let finished = false;

  return {
    [Symbol.iterator]() { return this; },
    next() {
      if (finished) return DONE;
      const chunk = [];
      while (chunk.length < size) {
        const step = source.next();
        if (step.done) { finished = true; break; }
        chunk.push(step.value);
      }
      return chunk.length > 0 ? { done: false, value: chunk } : DONE;
    },
    return() {
      if (!finished) { finished = true; source.return?.(); }
      return DONE;
    }
  };
}

function windows(iterable, size, undersized = "only-full") {
  const source = iterable[Symbol.iterator]();
  checkSize(source, size);
  if (undersized !== "only-full" && undersized !== "allow-partial") {
    fail(source, new TypeError('undersized must be "only-full" or "allow-partial"'));
  }
  const buffer = [];
  let finished = false;

  return {
    [Symbol.iterator]() { return this; },
    next() {
      while (!finished) {
        const step = source.next();
        if (step.done) {
          finished = true;
          if (undersized === "allow-partial" && buffer.length > 0 && buffer.length < size) {
            return { done: false, value: [...buffer] };
          }
          break;
        }
        buffer.push(step.value);
        if (buffer.length > size) buffer.shift();
        if (buffer.length === size) return { done: false, value: [...buffer] };
      }
      return DONE;
    },
    return() {
      if (!finished) { finished = true; source.return?.(); }
      return DONE;
    }
  };
}

function includes(iterable, searchElement, skip = 0) {
  const source = iterable[Symbol.iterator]();
  if (typeof skip !== "number" || (!Number.isInteger(skip) && Math.abs(skip) !== Infinity)) {
    fail(source, new TypeError("skip must be an integer"));
  }
  if (skip < 0 || (skip !== Infinity && skip > Number.MAX_SAFE_INTEGER)) {
    fail(source, new RangeError("skip must be between 0 and 2^53 - 1"));
  }

  let skipped = 0;
  for (let step = source.next(); !step.done; step = source.next()) {
    const value = step.value;
    if (skipped < skip) skipped += 1;
    else if (value === searchElement || (value !== value && searchElement !== searchElement)) {
      source.return?.(); // stop at the first match, closing the source like the native method
      return true;
    }
  }
  return false;
}

function join(iterable, separator) {
  const sep = separator === undefined ? "," : `${separator}`;
  let result = "";
  let first = true;
  for (const value of iterable) {
    if (!first) result += sep;
    first = false;
    if (value !== undefined && value !== null) result += `${value}`; // a Symbol throws, as in Array.prototype.join()
  }
  return result;
}
[...chunks([1, 2, 3, 4, 5], 2)];           // [[1, 2], [3, 4], [5]]
[...windows([1, 2, 3, 4], 2)];             // [[1, 2], [2, 3], [3, 4]]
includes(new Set(["a", NaN]), NaN);        // true
join(new Map([["a", 1], ["b", 2]]).keys(), " / "); // "a / b"

To use the native method when it exists, pick one at the call site rather than patching Iterator.prototype:

const toChunks = (iterator, size) =>
  typeof iterator.chunks === "function" ? iterator.chunks(size) : chunks(iterator, size);

Common Pitfalls

1. Calling chunks() or windows() on an Array

[1, 2, 3].chunks(2) throws, because arrays don't have chunks() or windows(); only iterators do. (Arrays do have their own includes() and join().) Call .values() first, or use Iterator.from() for anything else that's iterable.

2. Passing a String as the Size

chunks("3") and windows("3") throw a TypeError rather than converting the string. Sizes read from form fields or URL parameters need Number() first, and 0 or a negative number throws a RangeError.

3. Expecting a Short Window

With fewer values than the window size, windows() yields nothing by default. Pass "allow-partial" if a short input should still produce one window, and remember it never adds short windows at the end of a longer input.

4. Reusing an Iterator After includes() or join()

Both consume the iterator. After join() it's empty, and after includes() returns true a generator has been closed. Create a fresh iterator (call .values() or the generator again) for each question you ask.

5. Treating the Second Argument of includes() as an Array Index

It's a number of values to skip. It can't be negative and must be an integer, so includes(x, -1) and includes(x, 0.5) throw instead of behaving as they would on an array.

6. Searching an Endless Iterator

includes() on an iterator that never ends only returns if the value turns up. Put a take(n) in front of it to cap how far it looks.

Where Should You Use This?

  • chunks(): batching API calls, database inserts or queue messages; laying out grid rows and calendar weeks; paging through results on the client.
  • windows(): moving averages and other running statistics; differences or comparisons between neighbours; spotting runs, such as three failures in a row.
  • includes(): asking whether a stream, generator or Map/Set iterator contains a value without collecting it into an array, and stopping as soon as the answer is known.
  • join(): building CSV lines, query strings or log output from any iterator.

Reach for something else when you already have an array and need index-based access (slice() in a loop is just as clear), or when the test is a condition rather than one value, which is what some() and find() are for.

Frequently Asked Questions

Key Takeaway

chunks(n) and windows(n) turn any iterator into lazy groups of n values, without overlap and with overlap respectively, and only hold one group at a time. includes() and join() answer a question about an iterator directly: includes() stops and closes the source at the first SameValueZero match, and join() behaves exactly like the array method. Sizes must be integers, short inputs give no windows unless you pass "allow-partial", and the second argument of includes() is a skip count, never negative. Until every engine has them, detect each method separately and fall back to the plain functions above.

Changelog

  • — Added cross-reference to snippet 'First N Matches from Any Iterable with `filter()` and `take()`'
  • — Added cross-reference to tutorial 'Iterator Helpers: Lazy Pipelines over Any Iterator'
  • — Initial publication.
This tutorial is licensed under CC BY 4.0 by the author.

Share this tutorial

Spotted a mistake or something outdated in this tutorial? Suggest a correction.