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

359
Visualizações
Should I call new TextEncoder for the each encode operation?

I want to get a Uint8Array representation of the string. Is it better to preallocate it once and reuse or to create a new one per the encoding?

function stringToUtf8ByteArray(str) {
    return new TextEncoder("utf-8").encode(str)
}
var TE = new TextEncoder("utf-8")
function stringToUtf8ByteArray(str) {
    return TE.encode(str)
}

I guess, there won't be any race conditions, because js is single threaded thing, isn't it?

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

0

The TextEncoder doesn't contain anything that would make it costly to create, so the benefit of reusing an instance would be very small. The call to the encode method is doing the heavy lifting, so creating the TextEncoder each time would normally not be a concern.

You are right that there would be no race conditions if you were to preallocate the instance.

If you really need to trim the performance of the code, you should look into the encodeInto method, that places the result in an array that you provide. Reusing that array could do more for performance than reusing the TextEncoder instance.

The encodeInto method is currently not supported in Opera, so you would likely need the polyfill that is provided on the page that I linked to.

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