Flattening converts V8’s reference-based ConsString tree into a sequential string containing a full copy of its contents. V8 accepts extra allocation when direct access, indexing, slicing, or printing benefits from contiguous data. section 2.2.3
The mechanism follows a practical trade-off:
- Prefer sequential storage when operations repeatedly need fast access to string contents. section 2.2.3
Forcing flattening before output
A benchmark compares writing many small concatenations directly with writing strings passed through flatstr(s), whose s | 0 operation encourages flattening before the stream consumes them.
