Browse documentation

TextDecoder decodes UTF-16BE and UTF-16LE natively

Date: 2026-12-03 Area: web, encoding, generated-data Refines: ADR 0233, which left UTF-16's labels a RangeError Related: #2583, #2491, ADR 0250, Built-ins — TextDecoder

Context

ADR 0233 left UTF-16, x-user-defined and the legacy multi-byte encodings out, each to be added as its own decoder when something asked for it. web-platform-tests' Encoding suites (#2491) now run in the test suite, and 65 of their subtests decode UTF-16LE or UTF-16BE, including "utf-16", which names UTF-16LE. The standard's shared UTF-16 decoder keeps state between bytes, as the UTF-8 decoder does: a leading byte and a leading surrogate. UTF-8 is decoded natively, because a decoder written in the prelude was too slow under heaptrc (#2351).

Decision

  • UTF-16BE and UTF-16LE decode in the host, beside UTF-8. utf16Decode follows the standard's shared UTF-16 decoder and shares utf8Decode's driver: a counting pass charged to the limits, one string allocated at the counted length and charged before it is made, a writing pass, and a copy of a SharedArrayBuffer's bytes. As for UTF-8, a streaming decode keeps the undecoded bytes, at most three (a lead surrogate's two and one byte after it), and decodes them again at the start of the next call, which gives what the standard's kept state gives.
  • The two encodings follow the single-byte encodings in Generated.EncodingData, after x-user-defined, so their labels are looked up as every other label is. The generator writes ENCODING_SINGLE_BYTE_LAST, ENCODING_UTF16BE and ENCODING_UTF16LE, and singleByteDecode accepts only the encodings up to the first.
  • TextDecoder strips a leading U+FEFF for UTF-16 as it does for UTF-8. The legacy multi-byte encodings stay out (#2585).

Consequences

  • new TextDecoder("utf-16") and every other UTF-16 label decode, and encoding answers "utf-16le" or "utf-16be".
  • The UTF-8 and UTF-16 decoders share one driver, so a change to how decoding is charged or allocated reaches both.