Module 3 · Strings and Template Literals
Under the Hood: Immutability, Ropes and UTF-16
ProReading
Maria's badge printer from lesson 01 still prints by name.length, so "মাহি" gets four boxes and a name with 👋🏽 gets four boxes for one picture. Kenji has a second worry: the badge text is built with +=, and he read that += in a loop is slow. Both questions have the same answer: look at what the engine actually stores. This lesson opens V8, the engine inside Node, and then the Unicode standard, and fixes the printer for good.
Checking what this lesson needs for you…
What is inside Under the Hood: Immutability, Ropes and UTF-16
- A string never changes, so it can be shared
- What += costs on paper
- The rope: V8's ConsString
- The performance lab: += against push and join
- One-byte and two-byte strings
- UTF-16: code points and surrogate pairs
- Walking by code point
- Graphemes: what a reader sees
- Normalization: one letter, two spellings
- Lone surrogates: isWellFormed and toWellFormed