CKTIS / JavaScript
TextEncoder.encode and TypedArray.byteLength: measuring UTF-8 bytes
Encode text as UTF-8 and read the resulting byte count. Distinguish that count from UTF-16 code units, typed-array elements, and the size of the entire backing buffer.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
const encoder = new TextEncoder();
const bytes = encoder.encode(text);
const byteCount = bytes.byteLength;
// With no argument, encoder.encode() returns an empty Uint8Array.
// For any typed-array instance: view.byteLengthTextEncoder.encode and TypedArray.byteLength answer two separate questions: what bytes represent this text, and how many bytes does the resulting view span? new TextEncoder().encode(text) calls an instance method that encodes a string as UTF-8 only and returns a new Uint8Array. With no argument it returns an empty Uint8Array. Each lone surrogate is replaced with U+FFFD rather than rejected as an error.
view.byteLength is a read-only property, not a method. It counts bytes in the view; view.length counts elements, and view.buffer.byteLength counts bytes in the entire backing buffer. For an in-bounds view, view.byteLength equals view.length * view.BYTES_PER_ELEMENT. In a Uint8Array, each element is one byte. text.length counts UTF-16 code units, not UTF-8 bytes. Use bytes.byteLength when a limit is defined in UTF-8 bytes.
ECMAScript 2024 specifies the typed-array property. TextEncoder is a web-platform API, and its availability depends on the runtime. With a resizable buffer, a length-tracking view can change size. Reading byteLength returns 0 if the buffer is detached or the view is out of bounds. Calling the getter on an object that is not a typed array throws TypeError.
Examples
{
const text = "Aé🙂";
const bytes = new TextEncoder().encode(text);
console.log(`text.length=${text.length}; bytes.length=${bytes.length}; bytes.byteLength=${bytes.byteLength}`);
console.log(bytes.join(","));
const view = new Uint16Array(new ArrayBuffer(12), 2, 3);
console.log(`view.length=${view.length}; view.byteLength=${view.byteLength}; view.buffer.byteLength=${view.buffer.byteLength}`);
}Result
text.length=4; bytes.length=7; bytes.byteLength=7 65,195,169,240,159,153,130 view.length=3; view.byteLength=6; view.buffer.byteLength=12
`Aé🙂` takes 4 UTF-16 code units and 7 UTF-8 bytes. The `Uint16Array` view has 3 elements of 2 bytes each, so it spans 6 of the buffer's 12 bytes.
Related lessons
- JavaScript · Lesson 17 · Advanced · Version: 4