I was reading the %TypedArray%.from
spec and I noticed that it works differently from Array.from
: it first fully iterates its iterable argument, and then it maps every value.
The difference can be observed with this code:
function* generate() {
for (let i=0;i<3;i++) {
console.log(`Generate ${i}`);
yield i;
}
}
function map(v) {
console.log(`Map ${v}`);
return v;
}
console.log("Array.from");
Array.from(generate(), map);
console.log("%TypedArray%.from");
Uint8Array.from(generate(), map);
It will produce this output:
Array.from
Generate 0
Map 0
Generate 1
Map 1
Generate 2
Map 2
%TypedArray%.from
Generate 0
Generate 1
Generate 2
Map 0
Map 1
Map 2
I tried looking at older versions of the spec to see why they work differently, but this difference was already in the July 15, 2013 ES6 draft (which is the draft introducing %TypedArray%.from
and iterables support in Array.from
).
Does anyone know what is the reason behind this difference, or if there isn't a reason?