Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda