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
