Skip to content

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

Closed

Description

@bartlomieju

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 #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.

Activity

  1. added 3 commits that reference this issue on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions