tc39/proposal-arraybuffer-base64

Unavoidable copy in `toBase64`

0f-0b opened this issue · 1 comments

0f-0b commented

The spec currently reads:

  1. Let toEncode be ? GetUint8ArrayBytes(O).
  2. Let opts be ? GetOptionsObject(options).
  3. Let alphabet be ? Get(opts, "alphabet").

Step 2 makes a copy of the input. Step 4 calls user code, potentially modifying the contents of the original ArrayBuffer or detaching it. The subsequent steps base64-encode the copy, not the original. This means that an implementation won't be able to omit the copy in all cases.

The polyfills in this repository and in core-js instead directly base64-encode the input after reading options without ever copying. Should the spec be changed to match?

Oh, good catch. I agree. The this should be validated to be a Uint8Array as the first step, but only read from after any potential side effects.