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

283
Vistas
Is it a good practice to rename global variables and methods for minification

Let's say I have used document and setAttribute several times throughout the code.

Now, if I declare these in the beginning,

const _document     = document,
      _setAttribute = 'setAttribute';

and then later in the script, use them as,

_document.querySelector('.class')[_setAttribute]('data-something', '');

will it be fine, or will it have some flaws?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

IMO saving a couple bytes shouldn't really matter to you... Today's internet access is very fast and there are so many tools that can do it for you after you code, you are only slowing your development efforts by doing this. Code will be harder to read, harder to maintain. Also between _document and document you are using up more characters than the original variable... To what use?

Also note that your file can be served gzipped which will have a great impact on your file size.

Unless you are using a good http/2 server, https negotiation will likely take up a good chunk of the loading time for a medium sized file. I timed my bank's website, to serve a 44KB javascript file, it took 347ms*. From these 347ms, it took only 117 to download the file. The rest was mostly finding the server (DNS) and negotiating the connection (SSL). Shaving 50% off of the file size would hypothetically remove 59ms of download time. Putting the request length at 288 an effective 17% faster request.

enter image description here

All in all, this kind of optimization will certainly hurt your development efforts. Since there are already great tools to minimize at the end, there is no immediate need to harm your productivity by doing it yourself. Rules in optimizations are measure, measure, measure. So once your application/code is done, test its download speed, and performance. Then see which one you want to optimize and focus there.

*I'm in the train on my PC using internet that my phone shares through bluetooth.

[EDIT] Also, if you want to keep maintainability and reduce code size, you could create utility function, like if your example is used quite a lot it might help.

function setAttributeSelector(selector,attribute,value){
    document.querySelector('.class').setAttribute('data-something', '');
}

//and use it
setAttributeSelector('.class','data-something','');

But do this only if it makes sense to your codebase.

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