Web APIs

TextEncoderStream()

Try the free JavaScript compiler

Create a transform with a writable string side and readable UTF-8 byte side. It handles a surrogate pair split across successive string chunks. Example console output: 3.

Formal Syntax & Structure

javascript syntaxjavascript
new TextEncoderStream()

Return Value

TextEncoderStream

UTF-8 encoding transform.

Working Example

Example Snippetjavascript
(async () => {const source=new ReadableStream({start(c){c.enqueue("A");c.enqueue("é");c.close()}}); let size=0; for await(const bytes of source.pipeThrough(new TextEncoderStream())) size+=bytes.length; console.log(size);})()
JavaScript Reference