liveBook · Manning

UTF-32

As explained in JavaScript in Depth

A Unicode transformation format that represents each code point using 32 bits.

Why does a simple word take four times as many bytes in UTF-32 as in ASCII? UTF-32 assigns every Unicode code point a fixed 32-bit, four-byte representation, even when its value needs only one byte. section 2.1.2

Encoding a small value

The lowercase letter “a” has code point 97. UTF-32 represents it directly as 0x00000061: the numeric value is unchanged, but the representation occupies four bytes. section 2.1.2

Table 2.1 Comparing the ASCII and UTF-32 encodings of the word hello
Method
Encoding
ASCII
0x68656c6c6f
UTF-32
0x00000068000000650000006c0000006c0000006f

Compare the encodings of “hello.” Each UTF-32 code point occupies four bytes, so this ASCII-compatible word takes four times the space. section 2.1.2

That direct mapping makes UTF-32 easy to reason about, but costly for text dominated by small-valued code points. UTF-16 and UTF-8 are more broadly used for their efficiency and flexibility. Do not infer from UTF-32’s simplicity that a JavaScript string is stored as UTF-32; language behavior and runtime implementation are separate questions. section 2.1.2 section 2.0

How JavaScript in Depth covers it

How deep it goes
Defines its four-byte representation and works through its space cost, without detailing JavaScript string storage.
Examples
Shows “a” as 0x00000061 and compares “hello” in ASCII and UTF-32.
What it assumes
Assumes familiarity with bytes and hexadecimal notation while introducing code points and their UTF-32 representation.
What you can do after
Evaluate/understand it by recognizing when direct code-point representation costs more space than other encodings.
How it connects
Connects Unicode code points, ASCII, UTF-16, and UTF-8 through encoding size.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.