Why doesn't %TypedArray%.prototype.set consider to input an Iterable as an argument?

%TypedArray% constructor and %TypedArray%.from can accept an Iterable as an argument, but %TypedArray%.prototype.set ignores it.

Maybe because the length of an iterable is unknown. If the length of the array or array like + the offset param is beyond the length of the receiver a RangeError is thrown and no writes are performed.

With an iterable the iterator would first need to be stored in to a temp buffer to determine the length and then transferred.

That's correct. However, since %TypedArrray% constructor and %TypedArray%.from use IterateToList for the same reason, I don't think that's a reason.

1 Like

There is the difference that when constructing a TypedArray buffer the size limit is implementation defined:

Let db be a new Data Block value consisting of size bytes. If it is impossible to create such a Data Block, throw a RangeError exception.

https://tc39.es/ecma262/#sec-createbytedatablock

Whereas when calling .set the caller must ensure the items fit within the length of the receiver. The caller passing in an iterator is potentially a sign the caller has not checked how many items they are trying to write.

I am only speculating, maybe there were some discussions on this that can be found on github or the mailing list.

1 Like