ENCODING API
The Encoding API allows for converting between strings and typed arrays. The specification introduces four global classes for performing these conversions: TextEncoder
, TextEncoderStream
, TextDecoder
, and TextDecoderStream
.
Encoding Text
The Encoding API affords two ways of converting a string into its typed array binary equivalent: a bulk encoding, and a stream encoding. When going from string to typed array, the encoder will always use UTF-8.
Bulk Encoding
The bulk designation means that the JavaScript engine will synchronously encode the entire string. For very long strings, this can be a costly operation. Bulk encoding is accomplished using an instance of a TextEncoder
:
const textEncoder = new TextEncoder();
This instance exposes an encode()
method, which accepts a string and returns each character's UTF-8 encoding inside a freshly created Uint8Array...