Learn JavaScript

Lesson 5 of 9 · Strings and Template Literals

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

  1. A string never changes, so it can be shared
  2. What += costs on paper
  3. The rope: V8's ConsString
  4. The performance lab: += against push and join
  5. One-byte and two-byte strings
  6. UTF-16: code points and surrogate pairs
  7. Walking by code point
  8. Graphemes: what a reader sees
  9. Normalization: one letter, two spellings
  10. Lone surrogates: isWellFormed and toWellFormed