Skip to content
Basics

Unicode & UTF-8

The variable-width standard for global text representation.

Time Complexity
O(1)
Space Complexity
O(1-4 bytes)

Interactive Visualization

A
1 Byte Sequence
41
©
2 Byte Sequence
C2 A9
λ
2 Byte Sequence
CE BB
🌍
4 Byte Sequence
F0 9F 8C 8D

Technical Guide

UTF-8 is the dominant encoding standard today. It solved the problem of global character support while remaining space-efficient. It uses 1 to 4 bytes per character. ASCII characters take 1 byte, while complex symbols like Emojis take 4. This 'variable-width' nature makes it memory efficient for English but requires careful processing for indexing.

Logical Phases

  • 1
    Checking the first bits of a byte to determine the sequence length.
  • 2
    1-byte sequence starts with '0' (ASCII).
  • 3
    Multi-byte sequences use 'leading bits' (110, 1110, 11110) to signal total bytes.
  • 4
    Characters are decoded by combining bits across the sequence.

Implementation

const buf = Buffer.from('🌍');
console.log(buf.length); // 4 bytes

Expert Pitfalls

Confusing string.length (code units) with the actual number of bytes in UTF-8.