denoland/celld

Public

TextDecoder.decode is superlinear, so text() on a large body is slow

BA

## What

`TextDecoder.prototype.decode` builds its result one character at a time. `_decode8` in `crates/celld/js/text_encoding.js` appends with `s += String.fromCharCode(...)` per code point, so V8 accumulates a rope with a node per character. The cost grows faster than the input.

Measured in-Worker, decoding one buffer:

``` 1 MiB 50ms 2 MiB 120ms 4 MiB 693ms 8 MiB 1459ms ```

Reading the same bytes as a stream is 0-2ms at every one of those sizes, so the decoder is the whole cost.

## Why it matters

Any Worker that calls `request.text()`, `request.json()`, `response.text()` or `new TextDecoder().decode(...)` on a body of a few megabytes pays this. A JSON API accepting a 8 MiB document spends over a second inside the decode alone, and the transient rope is large enough to matter to a node's memory ceiling.

This was found while fixing denoland/celld#152. That issue is fixed by not decoding a body nobody asked for, which removes the cost from the pass-through path; a caller that genuinely wants the text still pays it.

## Expected

`decode` should build the string in batches rather than per character, so its cost is proportional to the input. Decoding a few megabytes should take milliseconds.

## Note

`_decode16` and `_decodeLegacy` in the same file build their results the same way and are worth checking together.

  1. bartlomiejuclosed3 months ago on GitHub

  2. bartlomiejureferenced 9c0ca09ef5d53 months ago on GitHub

  3. bartlomiejureferenced bc0e29cddf303 months ago on GitHub

  4. bartlomiejureferenced 89e4ffc53a143 months ago on GitHub