Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Why is that JavaScript's strings are using UTF-16 but one character's actual size can be just one byte?

according to this article:

Internally, JavaScript source code is treated as a sequence of UTF-16 code units.

And this IBM doc says that:

UTF-16 is based on 16-bit code units. Therefore, each character can be 16 bits (2 bytes) or 32 bits (4 bytes).

But I tested in Chrome's console that English letters are only taking 1 byte, not 2 or 4.

new Blob(['a']).size === 1

I wonder why that is the case? Am I missing something here?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Internally, JavaScript source code is treated as a sequence of UTF-16 code units.

Note that this is referring to source code, not String values. String values are referenced to also be UTF-16 later in the article:

When a String contains actual textual data, each element is considered to be a single UTF-16 code unit.

The discrepancy here is actually in the Blob constructor. From MDN:

Note that strings here are encoded as UTF-8, unlike the usual JavaScript UTF-16 strings.

about 4 years ago · Juan Pablo Isaza Relatório

0

UTF has a varying character size.

a has a size of 1 byte, but ą for example has 2

console.log('a', new Blob(['a']).size)
console.log('ą', new Blob(['ą']).size)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda