v153 · javascript · iterator helpers
Iterator.prototype.join
Iterator helpers gave iterators map, filter and take, but nothing to finish with. Turning a pipeline into a string still meant Array.from(…).join() — building the whole array first, purely to throw it away. join is the missing terminal operation, and it accumulates as it goes.
concepts
-
Joining a pipeline
The method itself: a generator, a couple of helpers, and a separator. Run it against whatever this browser has — the native method if it is here, an explicitly labelled shim if it is not — and see both the string and the work it took.
-
What the separator does to your values
null,undefined, objects, symbols, a separator that is a number. Every rule here is read back from the platform's ownArray.prototype.joinrather than asserted, because that is the behaviour the proposal is defined to match. -
A log formatter
The practical shape: paste raw log lines, compose a pipeline of helpers over them, and get a formatted block out. The pipeline is the thing you edit;
joinis what makes it a string. -
The array you were building anyway
Measured, at your chosen size: materialise into an array and join it, or accumulate a string as the iterator yields. Same output, different peak footprint — which is the whole reason the method exists.
why it shipped
Iterator helpers are lazy by design: map, filter, take and friends pull one value at a time and never hold the sequence in memory. That property survives right up until you want a string, at which point Array.from(iterator).join(", ") undoes it — the array exists only to be joined, and its peak size is the length of the sequence.
The proposal closes that gap with the obvious method. It matches Array.prototype.join exactly, down to how it treats null and undefined, so there is nothing new to learn: it is the same operation without the intermediate array.
the API
function* readings() { /* … yields lazily … */ }
// Before: the array exists only so that join can be called on it.
const line = Array.from(readings()).filter(Boolean).join(" · ");
// After: nothing is materialised.
const line = readings().filter(Boolean).join(" · ");
// The separator defaults to "," exactly as it does on arrays.
[1, 2, 3].values().join(); // "1,2,3"
enabling it now
Chrome ships both methods behind V8's staging flag before they reach stable. Verified on Chrome 150 on this machine: with the flag off, Iterator.prototype.join, Iterator.zip and Iterator.zipKeyed are all undefined; with it on, all three are functions and every demo here takes the native path.
google-chrome --js-flags=--harmony # or --js-flags=--js-staging
There is no chrome://flags entry for this one — staged JavaScript features are enabled by passing the V8 flag on the command line. chrome://flags/#enable-experimental-web-platform-features does not enable them; that switch covers Blink, not V8, and was measured making no difference here.